question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[🐛 Bug]: XPATH div[10+] cannot retrieve text.

See original GitHub issue

What happened?

When retrieving absolute and relative XPATH nodes, a div greater than 9 will return an empty text value.

How can we reproduce the issue?

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")

driver = webdriver.Chrome(options=options, service=Service(ChromeDriverManager().install()))
driver.get("https://www.cntower.ca/lighting-schedule/")
colour9 = driver.find_element(by=By.XPATH, value='//*[@id="block-de-theme-content"]/div/article/div/div/div/div[1]/div/div/div/div/div/div[9]/article/div[1]/div[2]')
colour10 = driver.find_element(by=By.XPATH, value='//*[@id="block-de-theme-content"]/div/article/div/div/div/div[1]/div/div/div/div/div/div[10]/article/div[1]/div[2]')

print(colour9.text) #returns a string
print(colour10.text) #returns an empty string
driver.quit()

Relevant log output

N/A

Operating System

Ubuntu / Multiple

Selenium version

Python 3.9.6

What are the browser(s) and version(s) where you see this issue?

Chromium 93.0.4577.82

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 93.0.4577.82

Are you using Selenium Grid?

No

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
titusfortnercommented, Sep 2, 2022

I think there’s just something in your absolute XPath tripping you up.

Loop over this (or tweak for whatever you’re actually trying to capture):

driver.find_elements(by=By.CSS, value='div.row article h2')

Feel free to reach us at https://www.selenium.dev/support/#ChatRoom

1reaction
symonkcommented, Sep 1, 2022

@tallmega it looks to me like the tiles are not properly rendered / visible here until scrolling occurs on that particular URL so I’m not sure it’s a particular xpath & div index of >9, the elements are found but their visible text is likely empty? https://w3c.github.io/webdriver/#get-element-text. Whether thats:

  • A problem on this specific site
  • Working as intended inline with the spec
  • A straight up bug

I am not so sure 😉, need to do a bit of digging.

For example if you scroll to the bottom of the page, then query all the texts, likely prints out correctly.


Naive example to prove out the theory:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


with webdriver.Chrome(service=Service(ChromeDriverManager().install())) as driver:
    driver.get("https://www.cntower.ca/lighting-schedule/")
    elements = driver.find_elements("xpath", "//span[@class='field field--name-title field--type-string field--label-hidden']")
    for element in elements:
        driver.execute_script("arguments[0].scrollIntoView();", element)
        print(element.text)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Xpath is unable to find text - Stack Overflow
As this text has no particular div I am getting difficulty to get it. Selenium code I tried driver.findElement(By.xpath(".//*[@id='filterInfo']/ ...
Read more >
Help with Screen Scraping Problem - UI.Vision RPA
Hello, I am having the hardest time finding a solution to this problem. I am trying to screen scrape the data from a...
Read more >
Unable to locate Text element using XPath in Selenium [closed]
Sometimes elements are not accessible directly. So, in test automation services we try including the parent div to locate that element.
Read more >
How to verify a tooptip which ist hidden in a div-section?
Hello, I'm trying to verify a tooltip in an Angular website. Its not possible to acess this tooltip with the Object Spy.
Read more >
Robot cannot find a locator saved from Katalon Studio 7 #3389
Hi,. I am stuck trying to figure out why is it that robot returns error: "Element with locator 'xpath=//div[@id='root']/div/div[2]/div[2]/div/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found