我是selenium的新手,我尝试使用隐式等待条件并等到它没用。但它在单独执行时工作正常。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
browser=webdriver.Firefox()
browser.get('https://runningstatus.in')
i=0
element = WebDriverWait(browser, 10)
select=Select(browser.find_element_by_id('godate'))
select.select_by_index(i)
browser.find_element_by_tag_name('input').send_keys('12801',Keys.RETURN)
x=browser.find_element_by_css_selector('div.runningstatus-widget-content')
name=x.find_element_by_tag_name('p')
print name.text
答案 0 :(得分:0)
每当发生页面转换,或者预期会出现新元素时,您应该等待它存在于DOM中。如果你不这样做,你会经常超时。在您的情况下,请尝试以下...
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser=webdriver.Firefox()
browser.get('https://runningstatus.in')
i=0
element = WebDriverWait(browser, 10)
select=Select(browser.find_element_by_id('godate'))
select.select_by_index(i)
browser.find_element_by_tag_name('input').send_keys('12801',Keys.RETURN)
css = 'div.runningstatus-widget-content'
x = WebDriverWait(browser, 30).until(
EC.element_to_be_clickable(('css selector', css)))
print x.text