我正在尝试从html表中获取数据,该表似乎根据下拉列表的值(使用默认值)在表上加载数据。但是,当我在下拉列表中执行selenium click()时,表值会更改,但我得到的数据仍然来自旧页面。如何获取更新的值?这是我试过的代码。
注意:我正在尝试的网址是this one
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(url) # you can use the url linked above for example
if browser.find_element_by_xpath(
'//a[@id = "user-header-oddsformat-expander"]/span').text != 'IN Odds':
# choose a type of odd that you want from the top dropdown menu
browser.find_element_by_xpath(
'//a[@id = "user-header-oddsformat-expander"]').click()
browser.find_element_by_xpath(
'//ul[@id = "user-header-oddsformat"]/li/a/span[contains(text(), "IN Odds")]').click()
browser.implicitly_wait(10)
table = browser.find_elements_by_css_selector('div#odds-data-table')
for elem in table:
print elem.text
这里,即使在dong find_elements ...()之前执行click()之后,我仍然从原始加载的表中获取数据,而不是新表。我该怎么做?
答案 0 :(得分:2)
使用text_to_be_present_in_element()
等待“In Odds”作为下拉值:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "http://www.oddsportal.com/soccer/england/premier-league/aston-villa-chelsea-Ea6xur9j/?r=1#over-under;2"
browser = webdriver.Firefox()
browser.implicitly_wait(10)
wait = WebDriverWait(browser, 10)
browser.get(url)
if browser.find_element_by_xpath(
'//a[@id = "user-header-oddsformat-expander"]/span').text != 'IN Odds':
# choose a type of odd that you want from the top dropdown menu
browser.find_element_by_xpath(
'//a[@id = "user-header-oddsformat-expander"]').click()
browser.find_element_by_xpath(
'//ul[@id = "user-header-oddsformat"]/li/a/span[contains(text(), "IN Odds")]').click()
wait.until(EC.text_to_be_present_in_element((By.XPATH, '//a[@id = "user-header-oddsformat-expander"]/span'), "IN Odds"))
table = browser.find_elements_by_css_selector('div#odds-data-table')
for elem in table:
print(elem.text)