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

别等时光非礼了梦想. 提交于 2021-02-19 04:59:33

问题


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>

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


回答1:


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:

  • Using 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:

    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
    



回答2:


As the data you want to fetch is stored as text, you can fetch it using text method like:

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:

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



回答3:


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:

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

when you inspect and click on chropath you will see all xapths



来源:https://stackoverflow.com/questions/57574316/how-to-print-the-text-using-a-locator-from-the-span-in-selenium-webdriver-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!