python - 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
解决方案
的第一个元素//div[@class='titlu']
是隐藏的,如果使用,您将不会获得价值,.text
因为它只会提取可见文本,使用.get_attribute('textContent')
或选择第二个元素。
推荐阅读
- javascript - 如何创建多个切换按钮?
- android - com.test.test.LibraryBook 类未定义无参数构造函数
- javascript - 升级material-ui v3到v4导致redux表单Field组件报错'invalid prop component'
- python - 如何在 Python 中检测 SVG 链接是否有错误
- load-balancing - ceph RBD有负载均衡的能力吗?
- reactjs - React.js:为什么这个事件会在调用之前运行?
- javascript - 有没有办法插入
进入 - css - 如何在编译时替换 scss 文件中的选择器
- delphi - 如何让 Chilkat IMAP ListMailBoxes 报告可用邮箱名称?
- angular - Firebase SSR:延迟加载的路由不起作用