Python selenium xpath geting text is empty

痴心易碎 提交于 2021-02-10 17:48:45

问题


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


回答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.




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



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


来源:https://stackoverflow.com/questions/53463974/python-selenium-xpath-geting-text-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!