exec85
exec85

Reputation: 487

python selenium get text from element

how would I get the text "Premier League (ENG 1)" extracted from this HTML tree? (marked part) enter image description here

I treid ti get the text with xpath, css selector, class... but I seem to cant get this text extracted.

Basically I want to create a list and go over all "class=with icon" elements that include a text (League) and append the text to that list.

This was my last attempt:

def scrape_test():
    alleligen = []

    #click the dropdown menue to open the folder with all the leagues
    league_dropdown_menue = driver.find_element_by_xpath('/html/body/main/section/section/div[2]/div/div[2]/div/div[1]/div[1]/div[7]/div')
    liga_dropdown_menue.click()
    time.sleep(1)
    
    #get text form all elements that conain a league as text
    leagues = driver.find_elements_by_css_selector('body > main > section > section > div.ut-navigation-container-view--content > div > div.ut-pinned-list-container.ut-content-container > div > div.ut-pinned-list > div.ut-item-search-view > div.inline-list-select.ut-search-filter-control.has-default.has-image.is-open.active > div > ul > li:nth-child(3)')
    
    #append to list
    alleligen.append(leagues)
    
    print(alleligen)

But I dont get any output.

What am I missing here?

(I am new to coding)

Upvotes: 1

Views: 870

Answers (3)

Razvan
Razvan

Reputation: 387

If you know that your locator will remain on the same position in that list tree, you can use the following where the li element is taken based on its index:

locator= "//ul[@class='inline-list']//li[2]"
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, locator))).text

Upvotes: 1

Sashaank
Sashaank

Reputation: 964

try this

path = "//ul[@class='inline-list']//li[first()+1]"
element = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, path))).text

print(element)

the path specifies the element you want to target. the first // in the path means that the element you want to find is not the first element in the page and exists somewhere in the page. li[first()+1] states that you are interested in the li tag after the first li.

The WebDriverWait waits for the webpage to load completely for a specified number of seconds (in this case, 5). You might want to put the WebdriverWait inside a try block.

The .text in the end parses the text from the tag. In this case it is the text you want Premier League (ENG 1)

Upvotes: 2

rahul rai
rahul rai

Reputation: 2326

Can you try :

leagues = driver.find_elements_by_xpath(“//li[@class=‘with-icon’ and contains(text(), ‘League’)]”)
For league in leagues:
     alleligen.append(league.text)
print(alleligen)

Upvotes: 1

Related Questions