python selenium present_of_element_located超时

时间:2018-07-10 08:55:16

标签: python selenium selenium-webdriver

硒显示在一页中的两个元素,我可以找到一个,但另一个却出错了。

url:base_url

代码:

element1 = wait.until(EC.presence_of_element_located((By.ID, 'soudiv')))
print('element1:', element1)
element2 = wait.until(EC.presence_of_element_located((By.ID, 'cresult')))
print(element2)

控制台输出:

element1: <selenium.webdriver.remote.webelement.WebElement (session="562daf934cfc22c3a31b17759d9a7502", element="0.8160783993595944-2")>
Traceback (most recent call last):
File "/Users/msxf/Documents/pythonProject/autohome/spider.py", line 56, in <module> start()
File "/Users/msxf/Documents/pythonProject/autohome/spider.py", line 44, in start
element2 = wait.until(EC.presence_of_element_located((By.ID, 'cresult')))
File "/anaconda3/lib/python3.6/site- packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

html: html

1 个答案:

答案 0 :(得分:0)

我尝试了一下,它起作用了,这是我的Waits

...
from selenium.webdriver.support.ui import WebDriverWait

...

soudiv = WebDriverWait(driver, 5).until(
                           EC.presence_of_element_located((By.ID, 'soudiv')))
print(soudiv)
cresult = WebDriverWait(driver, 5).until(
                           EC.presence_of_element_located((By.ID, 'cresult')))
print(cresult)

输出

<selenium.webdriver.remote.webelement.WebElement (session="d9cf0d2b2875a6a939cc2de50421f1d1", element="0.4909859897090536-1")>
<selenium.webdriver.remote.webelement.WebElement (session="d9cf0d2b2875a6a939cc2de50421f1d1", element="0.4909859897090536-2")>