简体   繁体   English

如何使用 Selenium 和 Python 从 div class 中提取文本

[英]How to extract text from div class using Selenium with Python

I am trying to create a bot to pay some bills automatically.我正在尝试创建一个机器人来自动支付一些账单。 The issue is I can't extract the amount(text) under div class.问题是我无法提取 div class 下的金额(文本)。 The error is element not found.错误是未找到元素。 Used driver.find_element_by_xpath and WebDriverWait.使用了 driver.find_element_by_xpath 和 WebDriverWait。 Can you please indicate how to get the highlighted text-see the attached link?您能否指出如何获得突出显示的文本 - 请参阅附件链接? Thanks in advance.提前致谢。 Page_inspect Page_inspect

You can use -您可以使用 -

driver.find_element_by_xpath("//div[@data-ng-bind-html='vm.productList.totalAmt']").text

I have written XPath on the basis of your attached image.我已经根据您所附的图片编写XPath Use list indexing to get a target div.使用列表索引获取目标 div。 For Example -例如 -

driver.find_element_by_xpath("(//div[@data-ng-bind-html='vm.productList.totalAmt'])[1]").text

I believe there was some issue with your xpath.我相信您的 xpath 存在一些问题。 Try below it should work:在下面尝试它应该可以工作:

amount = WebDriverWait(self.driver, self.timeout).until( EC.presence_of_element_located((By.XPATH, '//div[starts-with(@class,"bill-summary-total")]//div[contains(@data-ng-bind-html,"vm.productList.totalAmt")]'))) 
print('Your amount is: {}'.format(amount.text)) 
return float(amount.text) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM