Vali
Vali

Reputation: 649

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 receiving an empty string even though the site is containing that text.

What have I tried:

wait = WebDriverWait(self.driver, 10)   
wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Ap. de lux 3 ")))
e = self.driver.find_element_by_xpath(html_data.xpath)

also:

wait = WebDriverWait(self.driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH, xpath)))
e = self.driver.find_element_by_xpath(xpath)

and also I've used and this type of wait:

self.driver.implicitly_wait(10)

How I'm getting the text at this moment:

self.driver.find_element_by_xpath(xpath).text

the problem that I've faced here is that the text is refusing to appear on some occasions and in others it does, even though the actually XPath is found and it exists already. Maybe is not loaded completely, can any of you give me some advice about how can I fix this?

UPDATE:

Also, I'm trying to get the location and size of that using selenium but both of them are going to be 0. Any idea how can I fix that?

with, height = self.driver.find_element_by_xpath(html_data.xpath).size x, y = self.driver.find_element_by_xpath(html_data.xpath).location

Upvotes: 1

Views: 2676

Answers (3)

Andersson
Andersson

Reputation: 52665

@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()

Upvotes: 1

QHarr
QHarr

Reputation: 84455

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()

Upvotes: 2

ewwink
ewwink

Reputation: 19154

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.

Upvotes: 5

Related Questions