3

I'm trying to get the data from a html table that seems to load data on a table based on the value of dropdown (with a default value). However, when I do the selenium click() on the dropdown, the table values change but the data I get is still from the old page. How do I get the updated values instead? Here, is the code I've tried.

Note: The URL I'm trying for example is 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

Here, even after performing the click() before dong find_elements...(), I still get the data from the originally loaded table, not the new one. How do I do that?

1 Answer 1

2

Wait for "In Odds" to be the dropdown value using text_to_be_present_in_element():

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.