【问题标题】:How to get value using python selenium of element that is not selectable如何使用不可选择的元素的python selenium获取价值
【发布时间】:2022-01-11 22:42:38
【问题描述】:

我正在尝试使用 Python selenium 获取值 570。

...
<div _ngcontent-lci-c225="" class="mr-4 bd-highlight">
    <span _ngcontent-lci-c225="" class="clickable ng-star-inserted" style="margin-right: 5px;">Bank</span>
    <!---->
    570
    </div>

我不确定它是什么以及为什么当我在 chrome 开发工具中突出显示它时,它不会在样式窗口中显示任何我通常会看到的东西。尝试谷歌搜索以获取未成功的含义。如果有人知道请给我解释一下。

我的代码:

driver.find_element(By.XPATH, "/html/body/app-root/ng-component/div[4]/mat-drawer-container/mat-drawer-content/mat-grid-list/div/mat-grid-tile[2]/figure/app-inventory-menu/div/div[1]/div[2]/div[2]/span").get_attribute('innerHTML')

它返回“银行”而不是我需要的实际数值。不知道我还能做什么?

【问题讨论】:

    标签: javascript python html css selenium


    【解决方案1】:

    感谢您的建议,我能够弄清楚。

    driver.find_element(By.CSS_SELECTOR, "div.inventory-row:nth-child(1) > div:nth-child(2) > div:nth-child(2)").text.strip("Bank")
    

    也许有更好的方法,但它对我有用。

    【讨论】:

      【解决方案2】:

      这是因为数字不包含在&lt;span&gt; 元素中,而是包含在其上方的&lt;div&gt; 元素中。根据您的代码,我能找到的最佳选择器是 CSS 选择器:"div.mr-4.bd-highlight"

      【讨论】:

      • 谢谢你,我能够解决这个问题!
      【解决方案3】:
      parent = driver.find_element(By.CSS_ELECTOR,"mr-4.bd-highlight")
      child = parent.find_element(By.TAG_NAME,"span")
      print (parent.text.replace(child.text, ''))
      

      尝试删除跨度文本。我知道有一种比使用 split 更好的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-03
        • 2022-07-22
        • 2018-09-29
        • 1970-01-01
        • 2022-01-05
        • 2018-03-28
        • 2018-06-16
        • 1970-01-01
        相关资源
        最近更新 更多