【发布时间】:2019-07-03 12:23:39
【问题描述】:
我尝试在 python 中结合 selenium 编写一个脚本来等待某个元素可用。我希望我的脚本等待的内容受验证码保护。我不想设置固定时间。所以,我需要它等到我自己解决。
我试过这样:
import time
from selenium import webdriver
URL = "https://www.someurl.com/"
driver = webdriver.Chrome()
driver.get(URL)
while not driver.find_element_by_css_selector(".listing-content"):
time.sleep(1)
print(driver.current_url)
driver.quit()
但是,脚本抛出错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
我怎样才能让我的脚本等到元素可用,无论需要多长时间?
【问题讨论】:
-
您是否尝试在 while 循环内的 try/except 块中运行 find_elment_by_css_selector 函数?
-
您可以在验证码存在时循环播放吗?
-
是的,我尝试过这种方式@QHarr。一旦我解决了该验证码,该脚本就会引发错误,因为该 while 循环不再存在,并且这一行
driver.find_element_by_css_selector(".listing-content")引发了相同的错误(考虑选择器包含验证码元素)。 -
在 try 块中是否相同?
-
我无法以正确的方式组织该 try/except 块。 try/except 块如何无限期运行?
标签: python python-3.x selenium selenium-webdriver web-scraping