2

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

1

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
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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

Hamza Lachi
  • 1,046
  • 7
  • 25