【问题标题】:How to retrieve the value 355.60 from the element using Selenium and Python如何使用 Selenium 和 Python 从元素中检索值 355.60
【发布时间】:2020-10-31 09:57:54
【问题描述】:

我正在使用 selenium chromedriver 自动填写表格。最后一步是复制最终价格。当我尝试从元素中获取文本时,我什么也没收到。

下面是相关元素的图片。我想从中提取价格。

picture of problem 1:

这个元素真的很大,有这么多属性我不知道如何开始

<input id="TotalRegularPayment" name="TotalRegularPayment" sr-money-focus-clear="" sr-money-value="" class="mousetrap form-control ng-pristine ng-untouched ng-valid ng-not-empty" tabindex="1" type="text" ng-model="$ctrl.field.value" ng-change="$ctrl.onChange()" ng-disabled="($ctrl.field.meta.is_read_only || $ctrl.isLockedDown)" disabled="disabled">

这些行:

print("read this")
print("---")
print(totalprice.text)    
print("---")
print(totalprice.get_attribute('innerHTML'))
print("---")
print(pricewrapper.text)
print("---")
print(pricewrapper.get_attribute('innerHTML'))

给出这个输出:

read this
---

---

---
Total Rental
£
excl. VAT
---
 <div class="col-sm-4 label-col"> <label for="TotalRegularPayment" class="ng-binding">Total Rental</label> </div> <div class="col-xs-10 col-sm-6 input-col"> <div class="input-group currency"> <span class="input-group-addon calculator-addon disabled-style" ng-class="{'disabled-style': ($ctrl.field.meta.is_read_only || $ctrl.isLockedDown)}"><currency-symbol class="ng-isolate-scope"><span class="ng-binding">£</span></currency-symbol></span> <input id="TotalRegularPayment" name="TotalRegularPayment" sr-money-focus-clear="" sr-money-value="" class="mousetrap form-control ng-pristine ng-untouched ng-valid ng-not-empty" tabindex="1" type="text" ng-model="$ctrl.field.value" ng-change="$ctrl.onChange()" ng-disabled="($ctrl.field.meta.is_read_only || $ctrl.isLockedDown)" disabled="disabled"> <!-- ngIf: $ctrl.field.meta.show_vat_label --><span class="input-group-addon vat-placeholder ng-binding ng-scope disabled-style" ng-if="$ctrl.field.meta.show_vat_label" ng-class="{'disabled-style': ($ctrl.field.meta.is_read_only || $ctrl.isLockedDown)}"> excl. VAT </span><!-- end ngIf: $ctrl.field.meta.show_vat_label --> </div> </div> <div class="col-xs-2 col-sm-2 text-center"> <sr-calculating-spinner field="$ctrl.field" calculator-id="$ctrl.calculatorId" on-calculate="$ctrl.calculate()" class="ng-isolate-scope"><!-- ngIf: $ctrl.field.meta.is_calculable && !$ctrl.quoteModel.is_quote_read_only --> <!-- ngIf: $ctrl.model.calculation_path === $ctrl.field.meta.name && $ctrl.model.is_calculating --></sr-calculating-spinner> </div> 

我想要的元素是总价,但我还打印了它的包装,看看我是否有任何有用的东西我没有。 你能告诉我如何打印价格吗?

【问题讨论】:

  • 您是否使用 xpath 来查找元素。你可以发布那个代码吗?我也没有看到元素中的实际价格。你在看不同的元素吗

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

要打印&lt;input&gt; 标记内的值,即355.60,因为元素是Angular 元素,您必须为WebDriverWait 诱导WebDriverWait,您可以使用以下任一种Locator Strategies:

  • 使用CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#TotalRegularPayment[name='TotalRegularPayment']"))).get_attribute("value"))
    
  • 使用XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='TotalRegularPayment' and @name='TotalRegularPayment']"))).get_attribute("value"))
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多