Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding an element within an element using Selenium Webdriver

I am scraping a website that has a list of football games generated using JavaScript. I have written the following line that creates a list of all the game elements on the page:

list = browser.find_elements_by_xpath('//*[@data-sportid="1"]')

If I then write

for game in list:
    print game.text

it prints all the text fields contained in each of the games (home team name, away team name, etc.)

However, the loop

for game in list:
    print game.find_element_by_xpath('//*[@class="home-team"]').text

prints the very first home team's name in the page for each iteration of the loop.

It appears that game.find_element_by_xpath is searching the entire page, and not just this game element, and so it keeps returning the first home team name on the page.

How can I search for a child element within each item of the list?

EDIT

Here is the exact page I am working on

like image 712
KOB Avatar asked Nov 29 '22 22:11

KOB


1 Answers

@Justin Bartz Thank you.

You were using XPath //*[@class="home-team"] but no matter what parent element you are using the // tells XPath to search the whole document not just the children of the parent element. Using the XPath .//*[@class="home-team"] with the period in front of the forward slashes (IE .//) it tells it to search under the current element only.

Hope this expanded explanation helps the understanding.

   driver = webdriver.Chrome()
    driver.get("https://www.betfair.com/exchange/football/coupon?id=2")
    list = driver.find_elements_by_xpath('//*[@data-sportid="1"]')
    for game in list:
        print(game.find_element_by_css_selector('span.home-team').text)

or

    driver = webdriver.Chrome()
    driver.get("https://www.betfair.com/exchange/football/coupon?id=2")
    list = driver.find_elements_by_xpath('//*[@data-sportid="1"]')
    for game in list:
        print(game.find_element_by_xpath('.//span[@class="home-team"]').text)
like image 162
Barney Avatar answered Dec 05 '22 13:12

Barney