1

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

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Well thanks it's working but I do have one more question, do you know how can I get also the position of that xpath using selenium? I mean if I'm just getting it like self.driver.find_element_by_xpath(html_data.xpath).location['x'] is not going to work, I mean the X value or Y will be 0 in both of this cases
you can't get position of hidden element. it better to create new post if you have another problem.
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()

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.