Python + Selenium)我怎样才能点击这个东西?

Python + Selenium) How can I get this thing clicked?

问题。 我是 Python 的新手,找不到点击此 link 的方法。 我的目标是一个一个地单击 link,但我在单击第一个 link 时卡住了。 我搜索了几次并尝试了更多,但我什至找不到问题所在! link引出一个新的window(Survey),下面是html结构。

<div id="bb_deployment6" class="stream_item active_stream_item" role="listitem" x-aria-selected="true" tabindex="0" style="padding-left: 20px;"><span class="stream_datestamp">1 hour</span><div class="stream_context">Survey <a href="#" onclick="popup.launch('https://websitename', 'instrumentResponseWindow')">[Today] Survey A</a>: Click to submit survey </div><div class="stream_details"></div><div class="stream_context_bottom"></div></div>

<div id="bb_deployment5" class="stream_item" role="listitem" x-aria-selected="false" tabindex="-1" style="padding-left: 20px;"><span class="stream_datestamp">2 hour</span><div class="stream_context">Survey <a href="#" onclick="popup.launch('https://Websitename2', 'instrumentResponseWindow')">[Today] Survey B</a>: Click to submit survey </div><div class="stream_details"></div><div class="stream_context_bottom"></div></div>

这是我试过的方法

  1. 第一枪

from selenium import webdriver
browser =webdriver.Chrome("C:\Pii\selenium\chromedriver.exe")

#Open the Site
browser.get("https://that site")

#Find & Click!!
browser.find_element_by_partial_link_text("Survey").click()

第一个错误代码是

:消息:没有这样的元素:无法定位元素:{"method":"partial link text","selector":"Survey"}

  1. 第二枪:OK 可能加载时间太短了?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser =webdriver.Chrome("C:\Pii\selenium\chromedriver.exe")
browser.get("https://that site")

#Wait & Click
wait = WebDriverWait(browser, 10)
element = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Survey")))
browser.find_element_by_partial_link_text("Survey").click()

现在它说

:selenium.common.exceptions.TimeoutException:消息:

  1. 第三枪:点击部分可能是onclick的问题?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser=webdriver.Chrome("C:\Pii\selenium\chromedriver.exe")

browser.get("https://that site")


wait = WebDriverWait(browser, 10)
element = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Survey")))


sample = browser.find_element_by_link_text("Survey")
browser.execute_script("arguments[0].click();",sample)

它说

:selenium.common.exceptions.TimeoutException:消息:

与上面相同的消息

  1. 第四枪:也许我应该使用 XPATH 而不是文本?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser=webdriver.Chrome("C:\Pii\selenium\chromedriver.exe")


browser.get("https://that site")

wait = WebDriverWait(browser, 10)
element = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="bb_deployment5"]/div[1]/a')))
sample = browser.find_element_by_xpath('//*[@id="bb_deployment5"]/div[1]/a')
browser.execute_script("arguments[0].click();",sample)

结果是一样的

我想我完全错了,但我不明白那是什么。 任何答案都会有很大的帮助。谢谢

由于该元素位于不同的 iframe,您应该将焦点切换到那个 iframe,然后搜索该元素。这就是你的做法:

iframe = browser.find_element_by_class_name('cloud-iframe')
browser.switch_to.frame(iframe)

element = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="bb_deployment5"]/div[1]/a')))
element.click()

这应该可行,但我不能 100% 确定,因为我自己还没有看过该网站。