gpt4 book ai didi

python - 如何使用 selenium webdriver python 中跨度的定位器打印文本?

转载 作者:太空宇宙 更新时间:2023-11-03 15:31:49 26 4
gpt4 key购买 nike

我正在使用 selenium 进行 UI 测试。我在下面检查了 chrome 浏览器的元素。

<div tabindex="-1" unselectable="on" role="gridcell" comp-id="2815" col-id="StartBaseMV" class="ag-cell ag-cell-not-inline-editing ag-cell-with-height cell-number ag-cell-value" style="width: 120px; left: 2020px; text-align: right; ">
<span>
<span class="ag-value-change-delta"></span>
<span class="ag-value-change-value">($5,281,158)</span>
</span>
</div>

我为编写 xpath 所做的尝试。

//div[@col-id="StartBaseMV" and @class="ag-cell ag-cell-not-inline-editing ag-cell-with-height cell-number ag-cell-value"]/span[@class="ag-value-change-data"]/span[@class="ag-value-change-value"]

但是,它不起作用。建议任何线索

最佳答案

你很接近。据推测,您正在尝试提取文本 ($5,281,158) 并为此需要为 visibility_of_element_located() 引入 WebDriverWait 并且您可以使用以下任一解决方案:

  • 使用 CSS_SELECTOR:

    print(WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ag-cell.ag-cell-not-inline-editing.ag-cell-with-height.cell-number.ag-cell-value[col-id='StartBaseMV'] span.ag-value-change-value"))).get_attribute("innerHTML"))
  • 使用 XPATH:

    print(WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//div[@col-id='StartBaseMV' and @class='ag-cell ag-cell-not-inline-editing ag-cell-with-height cell-number ag-cell-value']//span[@class='ag-value-change-value']"))).get_attribute("innerHTML"))
  • 注意:您必须添加以下导入:

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

关于python - 如何使用 selenium webdriver python 中跨度的定位器打印文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57574316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com