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

问题描述

我正在尝试创建一个自动支付一些账单的机器人。问题是我无法提取div类下的amount(text)。错误是找不到元素。 使用了driver.find_element_by_xpath和webdriverwait。您能否说明如何获得突出显示的文本-请参阅附件链接?预先感谢。Page_inspect

解决方法

您可以使用-

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

我已经根据您所附的图片写了XPath。使用列表索引获取目标div。例如-

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

,

我相信您的xpath出现了问题。尝试以下操作即可:

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)