首页 > 解决方案 > Python selenium xpath获取文本为空

问题描述

所以我有这个链接,我试图从这个 XPath 中获取文本,//div[@class='titlu']但由于某种原因,有时我得到的文本应该是这样的,有时我收到一个空字符串,即使网站包含该文本.

我尝试了什么:

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)

还:

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

而且我也使用过这种等待:

self.driver.implicitly_wait(10)

我现在如何获取文本:

self.driver.find_element_by_xpath(xpath).text

我在这里遇到的问题是文本在某些情况下拒绝出现,而在其他情况下却出现了,即使找到了实际的 XPath 并且它已经存在。也许没有完全加载,你们中的任何人都可以给我一些关于如何解决这个问题的建议吗?

更新:

另外,我正在尝试使用 selenium 获取它的位置和大小,但它们都将是 0。知道如何解决这个问题吗?

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

标签: pythonhtmlseleniumselenium-webdriverxpath

解决方案


的第一个元素//div[@class='titlu']是隐藏的,如果使用,您将不会获得价值,.text因为它只会提取可见文本,使用.get_attribute('textContent')或选择第二个元素。


推荐阅读