'Get text from popup menu using Python's Selenium

When you hover over the i you get a popup menu.

no hover

hover

I want to get the last item of the popup 3,0 using Python's Selenium.

But however, it's not working for me. What do I do wrong?

a = driver.find_element(By.XPATH, '//*[@id="articlelist"]/form/div[1]/div[2]/div/div/span/div/div[3]/div[2]').text
print(a)

HTML Snapshot: Website code

Full website code for download: we.tl/t-dZexrOqPU8



Solution 1:[1]

To print the text 3,0 you can use either of the following locator strategies:

  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//div[@class='subinfo']/div[@class='art_popup_infobox']/div[@class='art_popup_line_wrapper'][./div[starts-with(., 'Durchmesser')]]//div[@class='art_popup_info']").text)
    

Ideally to extract the text 3,0 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='subinfo']/div[@class='art_popup_infobox']/div[@class='art_popup_line_wrapper'][./div[starts-with(., 'Durchmesser')]]//div[@class='art_popup_info']"))).get_attribute("innerHTML"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium