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

How to extract text from div class using Selenium with Python

我正在尝试创建一个机器人来自动支付一些账单。问题是我无法提取 div class 下的金额(文本)。错误是找不到元素。 使用 driver.find_element_by_xpath 和 WebDriverWait。能否请您指出如何获得突出显示的文本-参见附件link?提前致谢。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)