WebDriver硒超时异常

时间:2019-03-16 15:21:05

标签: python selenium

关于此的第一篇文章,是Python的新手。我正在尝试学习如何从网站上抓取数据。首次加载网站时,将显示一个免责声明窗口,而我要执行的所有操作是使用browser.find_element_by_id按下“接受”按钮。

我正在使用webdriverwait命令等待页面加载,然后再单击“接受”按钮,但是我一直收到Timeoutexception。这是我当前拥有的代码:

from selenium import webdriver
#get the chrome webdriver path file
browser = webdriver.Chrome(executable_path=r"C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
browser.get('http://foreclosures.guilfordcountync.gov/')

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#wait until element is loaded
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))

element = browser.find_element_by_id("cmdAccept")
element.click()

这是我不断收到的错误:

Traceback (most recent call last):
  File "C:/Users/Abbas/Desktop/Foreclosure_Scraping/Foreclosure_Scraping.py", line 33, in <module>
    wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))
  File "C:\Users\Abbas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

我相信这与从网站中调出按钮本身的ID有关,但老实说我不知道​​。任何帮助是极大的赞赏。

1 个答案:

答案 0 :(得分:2)

您尝试定位元素的尝试失败,因为它们嵌套在iframe中。在尝试单击或以任何方式使用硒之前,必须告诉硒切换到包含所需元素的iframe。请尝试以下操作:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#get the chrome webdriver path file
browser = webdriver.Chrome(executable_path=r"C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
browser.get('http://foreclosures.guilfordcountync.gov/')
browser.switch_to.frame(browser.find_element_by_name("ctl06"))
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))
element = browser.find_element_by_id("cmdAccept")
element.click()