如何使用Selenium Webdriver Python中的跨度使用定位器打印文本? [英] How to print the text using a locator from the span in selenium webdriver python?

查看:62
本文介绍了如何使用Selenium Webdriver Python中的跨度使用定位器打印文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒进行UI测试.我在下面是chrome浏览器的inspect元素.

I am using the selenium for UI testing. I have below inspect element of chrome browser.

<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的尝试.

What I tried for writing 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"]

但是,它不起作用.提出任何线索

But, it's not working. Suggest any clue

推荐答案

您非常接近.大概您正在尝试提取文本($ 5,281,158),并实现为 visibility_of_element_located()引入 WebDriverWait 的功能,然后可以使用以下任一解决方案:

You were pretty close. Presumably you are trying to extract the text ($5,281,158) and to achieve that you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following solutions:

  • 使用 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"))
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

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

  • 这篇关于如何使用Selenium Webdriver Python中的跨度使用定位器打印文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆