Skip to content Skip to sidebar Skip to footer

Python Selenium Xpath Geting Text Is Empty

So I have this link and I'm trying to obtain the text from this XPath //div[@class='titlu'] but for some reason sometimes I'm getting the text how it should be and other times I'm

Solution 1:

the first element of //div[@class='titlu'] is hidden and you will not get value if using .text because it will only extract visible text, use .get_attribute('textContent') or select second element.

Solution 2:

You can execute script to access. I learnt this method from an answer by @pguardiario

from selenium import webdriver

d = webdriver.Chrome()
d.get("https://www.imobiliare.ro/inchirieri-apartamente/sibiu/hipodrom-4/apartament-de-inchiriat-3-camere-X84T100B2?lista=2361394")
items = d.execute_script("return [...document.querySelectorAll('div.titlu')].map(item => item.innerText)")
print(items)
d.quit()

Solution 3:

@QHarr answer returns required output (+1), but as alternative to that the same output can be achieved with common approach without using JavaScript executor:

from selenium import webdriver

d = webdriver.Chrome()
d.get("https://www.imobiliare.ro/inchirieri-apartamente/sibiu/hipodrom-4/apartament-de-inchiriat-3-camere-X84T100B2?lista=2361394")
items = [item.get_attribute('innerText') for item in d.find_elements_by_xpath("//div[@class='titlu']")]
print(items)
d.quit()

Post a Comment for "Python Selenium Xpath Geting Text Is Empty"