【问题标题】:Find element by xpath in selenium takes more time when NoSuchElementException encounters遇到 NoSuchElementException 时,在 selenium 中通过 xpath 查找元素需要更多时间
【发布时间】:2015-04-13 10:01:47
【问题描述】:

我正在使用 selenium 从网页中收集信息。有两种网页,一种需要按下按钮显示所需信息,另一种直接显示信息。

try:
    BrowserObj_dirver.find_element_by_xpath("//li[@id='tab-item-relationships']/a").click()
    test=BrowserObj_dirver.find_elements_by_xpath("//div[@class='enum']/a")
    for t in test:
        if t.text not in sha256:
            sql2="insert into sha2 values (%d,'%s')" % (virusId,t.text)
            sha256.append(t.text)
            cursor.execute(sql2)
            db.commit()
            virusId=virusId+1
            print t.text
except NoSuchElementException:
    print "no button"
    (and operation for no button page here)

但是遇到NoSuchElementException需要更多的时间,一般是7s -10s。是什么原因?

【问题讨论】:

  • 尝试将隐式等待时间设置为您想要的值,例如 BrowserObj_dirver.implicitly_wait(10) # seconds
  • 非常感谢。有用。我认为implicitly_wait 仅适用于之前的下一次搜索,但实际上它会影响之后的everyvsearch。谢谢。
  • 很高兴听到。将此信息添加为完整回复 - 如果您接受答案,问题将不会保持开放状态。

标签: python selenium xpath


【解决方案1】:

尝试将隐式等待时间设置为您想要的值,例如BrowserObj_dirver.implicitly_wait(10) # seconds

这将适用于驱动程序从此时开始执行的每个 findElement。当然,如果你将等待时间设置得很短,findElement 可能不会等待仍在加载的元素。

或者,您可以使用 WebDriverWait 的变体,初学者请参阅此文档:http://selenium-python.readthedocs.org/en/latest/waits.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-17
    • 2016-03-12
    • 1970-01-01
    • 2022-07-05
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多