【问题标题】:Selenium in Python expected conditions works with Firefox but not ChromePython 预期条件中的 Selenium 适用于 Firefox 但不适用于 Chrome
【发布时间】:2018-09-14 10:22:45
【问题描述】:

我登录到一个私人网站,然后等待链接文本出现,然后使用 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 的代码中做错了什么,导致它抛出超时异常?

【问题讨论】:

  • 如果您在当前版本的 Chrome 65 上运行,如果您没有运行 至少 chromedriver version 2.36,请更新您的 chromedriver 并执行您的测试再次。从HERE 更新。我已经看到很多关于新的稳定版本的 chrome 和旧版本的 chromedriver 的问题。
  • 那是我用的那个,但是我通过你提供的链接再次下载它并执行了测试,但仍然有同样的问题。我正在使用当前版本的 Chrome 65。
  • 我编辑了帖子说替换等待预期条件的睡眠计时器有效,但这不是我想要使用的。

标签: python google-chrome selenium firefox


【解决方案1】:

有几件事需要解决:

  • 最后,当您在 WebElement 上调用 click() 时,您应该使用子句 element_to_be_clickable(locator)强>.

  • 当您使用expected_conditions 子句作为element_to_be_clickable(locator) 时,返回WebElement,您可以直接在其上调用click() 方法。

  • 您优化的代码行将是:

    WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "OTD"))).click()
    

更新 A

根据您的评论更新,您看到了错误:

'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)

更新 B

根据您的评论更新,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()

【讨论】:

  • 谢谢你。我正在使用 browser.get 而不是单击,因为单击链接会在新选项卡中打开链接,这不是我想要的。另外,这给了我一个错误“'element_to_be_clickable'对象没有属性'click'”
  • 使用visibility_of_element_located 不适用于chrome 或firefox,因为它会抛出超时异常,但用presence_of_element_located 替换它在firefox 中有效,并且只在第一次使用chrome 时有效。我用 chrome 又试了大约 10 次,它一直抛出超时异常。我通过在页面中定位另一个元素找到了解决方案。我只是觉得我的代码适用于 firefox 但不适用于 chrome,这很奇怪。不过感谢您的帮助!
  • Selenium 确实与 Firefox 中的元素交互,有时在 Chrome 中,即使它无法获得 visibility_of_element。它在 Firefox 中使用 present_of_element 而不是 visibility_of_element 时有效。这就是为什么我认为我遇到的问题很奇怪。我会接受您的回答,因为它是促使我找到有效解决方案的原因。谢谢!
  • 现在说得通了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 2018-09-08
  • 2014-11-17
  • 2015-06-09
  • 2011-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多