简体   繁体   English

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

[英]How to print the text using a locator from the span in selenium webdriver python?

I am using the selenium for UI testing. 我正在使用selenium进行UI测试。 I have below inspect element of chrome browser. 我有以下检查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>

What I tried for writing xpath. 我尝试写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 建议任何线索

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: 据推测,您正在尝试提取文本($ 5,281,158)并实现这一点,您需要为visibility_of_element_located()引入WebDriverWait ,并且您可以使用以下任一解决方案:

  • Using CSS_SELECTOR : 使用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")) 
  • Using XPATH : 使用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 

As the data you want to fetch is stored as text, you can fetch it using text method like: 由于您要获取的数据存储为文本,因此您可以使用以下text方法获取它:

driver.find_element_by_class_name('ag-value-change-value').text

And if there are multiple elements with the same class name then you can use the xpath: 如果有多个具有相同类名的元素,则可以使用xpath:

driver.find_element_by_xpath("//div[@col-id='StartBaseMV']//span[@class='ag-value-change-value']").text

You want to replace this line: 您想要替换此行:

//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"]

to this line: 到这一行:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@col-id='StartBaseMV']//span[@class='ag-value-change-value']")))

You Can Also Use Chrome Extension Chropath For Xpath Here is the link: 你也可以使用Chrome扩展Chropath为Xpath这里是链接:

https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo

when you inspect and click on chropath you will see all xapths 当你检查并点击chropath时,你会看到所有的xapth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM