我登录到私人网站,然后等待链接文本出现,然后使用browser.get()打开链接。几乎每次等待链接文本“OTD”出现时,下面的代码都会抛出超时异常。奇怪的是,它在许多尝试中都有效。更奇怪的是,如果我使用睡眠定时器而不是等待预期的条件,它可以工作,但这不是我想要使用的。
这是html:
<a target="_blank" href="/privateurl">OTD</a>
以下是使用chromedriver的代码:
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
import webbrowser
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : 'E:\\folder'}
chrome_options.add_experimental_option('prefs', prefs)
browser = webdriver.Chrome(chrome_options=chrome_options)
#browser.get('private url login page')
#enter login information and submit
wait = WebDriverWait(browser, 10)
wait.until(
EC.presence_of_element_located((By.LINK_TEXT, "OTD"))
)
browser.get('private url')
我有相同的代码,但对于Firefox驱动程序而言,它每次都能完美运行。我只需要使用Chrome浏览器。
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
import webbrowser
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.dir", 'E:\\folder')
profile.set_preference("browser.download.useDownloadDir", True)
browser=webdriver.Firefox(profile)
#browser.get('private url login page')
#enter login information and submit
wait = WebDriverWait(browser, 10)
wait.until(
EC.presence_of_element_located((By.LINK_TEXT, "OTD"))
)
browser.get('private url')
我在使用chromedriver的代码中做错了什么导致它抛出超时异常?
答案 0 :(得分:1)
有几件事需要解决:
最后,当您在 WebElement 上调用click()
而不是expected_conditions
子句而不是presence_of_element_located()
时,您应该使用一词element_to_be_clickable(定位器)强>
当您使用expected_conditions
子句作为element_to_be_clickable(locator)
时,会返回 WebElement ,您可以直接在其上调用click()
方法。
您的优化代码行将是:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "OTD"))).click()
根据您的评论更新,您会看到错误:
'element_to_be_clickable' object has no attribute 'click'
另一种方法是提取href
属性并按如下方式调用get()
:
element = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.LINK_TEXT, "OTD")))
attr_href = element.get_attribute("href")
driver.get(attr_href)
根据您的评论更新 Selenium与元素互动的想法......即使无法获得visibility_of_element 也不是完整的证明解决方案:
expected_conditions
条款presence_of_element_located(locator)
提及:
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
locator - used to find the element returns the WebElement once it is located.
expected_conditions
条visibility_of_element_located(locator)
提及的地方:
class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
locator - used to find the element returns the WebElement once it is located and visible.
从 Selenium 角度来看,如果某个元素未显示 Selenium 将不会与该元素互动,例如调用click()