0

I am trying to retrieve the text of a specific element in a table with the following XPATH:

/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input

using

driver.maximize_window() # For maximizing window
driver.implicitly_wait(3) # gives an implicit wait for 20 seconds
driver.find_element(By.XPATH, value = "/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input").text()

but I get the following error:

NoSuchElementException: Message: Unable to locate element: /html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]/input

I have also tried accessing the element by CSS selector and value, without success. Unfortunately the link is secured so I cannot share it but here is a screenshot of the element

enter image description here

Arthur Langlois
  • 137
  • 1
  • 9
  • A good approach is often to set a breakpoint where the XPath expression is applied and to try shortened versions of the expression, to find where it breaks exactly. I.e. does `/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]/td[7]` work? Does `/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[2]`? Etc. – Grismar Feb 13 '23 at 22:32

2 Answers2

4

Instead of your absolute XPath, try relative XPath. Below is the expression:

//input[@name='AdjIncrementAmount_1']

from the screenshot, I am not really sure if the value of attribute name (after the text Amount) has one _ or two __. If the above with(one _) doesn't work, try with 2 as below:

//input[@name='AdjIncrementAmount__1']

Shawn
  • 4,064
  • 2
  • 11
  • 23
  • Agree 100%. Using relative paths by finding things based on ClassName, ID or even text content will always be better than providing the full path through the DOM tree. If you are using chrome, you can test your Xpath expression in your dev console for instant feedback rather than using the web driver to test. To do this, you would call the `$x()` function and provide your xpath as a param for example: `$x("//div") //Xpath expression that returns all divs on the page`. – Joseph Gast Feb 13 '23 at 22:42
1

To print the text $36,400.00 you can use either of the following locator strategies:

  • Using css_selector:

    print(driver.find_element(By.CSS_SELECTOR, "td > input[name='AdjIncrementAmount__1']").get_attribute("value"))
    
  • Using xpath:

    print(driver.find_element(By.XPATH, "//td/input[@name='AdjIncrementAmount__1']").get_attribute("value"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.common.by import By
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352