0

Here is the HTML. I want to use the dynamic xpath here to get the text of the header elements.

<div class="w3-bar w3-theme w3-card-2 w3-wide notranslate">
<a class="w3-bar-item w3-button w3-hover-white w3-padding-16 hidesm" onclick="w3_open()" href="javascript:void(0)">
<a id="navbtn_tutorials" class="w3-bar-item w3-button barex w3-hover-white w3-padding-16" onclick="w3_open_nav('tutorials')" href="javascript:void(0)">
TUTORIALS
<i class="fa fa-caret-down"></i>
<i class="fa fa-caret-up" style="display:none"></i>
</a>
hElements = "//div[@class='w3-bar w3-theme w3-card-2 w3-wide notranslate']//a" 
#is my xpath fetches all the header elements
hElementsSize = len(browser.driver.find_elements_by_xpath(hElements)) 
#fetches the size of the header elements
print("Total Header Elements:", hElementsSize)
hElementsText = browser.driver.find_element_by_xpath(hElements).text
print(hElementsText) 
#i'm not able to get the text of the header elements
Guy
  • 34,831
  • 9
  • 31
  • 66
  • welcome to SO! please read [mcve] and edit your post accordingly. Right now the html you provided is incomplete edit it and format it like html – Dev Mar 03 '20 at 07:52

3 Answers3

0

The way you are expecting text using browser.driver.find_element_by_xpath will return first element's text which is empty as i can see in HTML shared by You. findElement locates the first element in the DOM in case multiple elements are there.

Need to use find_elements and loop through to check expected one

hElements = "//div[@class='w3-bar w3-theme w3-card-2 w3-wide notranslate']//a"
listOfElement = driver.find_elements_by_xpath(hElements)
print("Total Header Elements:", len(listOfElement))

for element in listOfElement:
    print(element.text)
NarendraR
  • 6,770
  • 7
  • 35
  • 69
  • thanks a lot. I could only fetch text of 4/8(out of 1 is empty element) other 3 elements are like icons/images. But on mouse hover the title of the icon displays. Here is the HTML for the other 3 elements(icons). – Satish Ghanta Mar 03 '20 at 16:06
0

If you want to return them all, assuming your xpath is correct, you would have to use find_elements_by_xpath. Then it returns a list of all the elements it found.

hElementsText = browser.driver.find_elements_by_xpath(hElements)
for el in hElementsText:
    print(el.text)
010011100101
  • 1,203
  • 6
  • 18
  • thanks a lot. I could only fetch text of 4/8(out of 1 is empty element) other 3 elements are like icons/images. But on mouse hover the title of the icon displays. Here is the HTML for the other 3 elements(icons). – Satish Ghanta Mar 03 '20 at 16:02
0

The desired element is a JavaScript enabled element, so to retrieve the text from the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and get_attribute() method:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.w3-bar.w3-theme.w3-card-2.w3-wide.notranslate a#navbtn_tutorials"))).get_attribute("innerHTML"))
    
  • Using XPATH and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='w3-bar w3-theme w3-card-2 w3-wide notranslate']//a[@id='navbtn_tutorials']"))).text)
    
  • 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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217