1

I would like to select a cell value with a known row index and selecting it by header name (here "Adjusted Amount")

My code is as follows:

driver.find_element(By.XPATH, value = f"/html/body/form[2]/table/tbody/tr/td/table/tbody/tr[{2}]/td[th[text()='Adjusted Amount']]/input").get_attribute('value')

but still get a NoSuchElementException.

Snapshot of the HTML:

This is what the page source looks like

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Arthur Langlois
  • 137
  • 1
  • 9
  • what is the URL that you are trying to select this text from? what does the HTML look like? It's hard to diagnose a problem just based on what you have given. – Andrew Ryan Mar 12 '23 at 04:41
  • I cannot share the link, but I just uploaded the post with a screenshot of the HTML source. – Arthur Langlois Mar 12 '23 at 05:04
  • it doesn't look like there are any `th[text()='Adjusted Amount']` in the HTML that you show. Are you trying to select the input source below the Adjusted Amount header? (could you also show that element in the HTML) – Andrew Ryan Mar 12 '23 at 05:12
  • 1
    Screenshots of the UI are great, screenshots of code or HTML are not. Please read why [a screenshot of code/HTML is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the HTML as text and properly format it instead. – JeffC Mar 12 '23 at 05:38
  • It's impossible for anyone to test and debug your XPath without access to the actual HTML which is your source data. A picture of the data is not enough. – Conal Tuohy Mar 12 '23 at 11:35

1 Answers1

1

Given the HTML:

html

There are a couple of issues in your line of code which you need to address as follows:

  • The element with text Adjusted Amount is within an <iframe> so you have to induce WebDriverWait for the desired frame_to_be_available_and_switch_to_it.

     WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#WorkArea")))
    
  • The element with text Adjusted Amount isn't within any <th> but within <td>. So to reference the <td> element ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "form[name*='TotalBondCalculation table table > tbody > tr td:nth-child(7)']")))
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//form[contains(@name, 'TotalBondCalculation')]//table//table/tbody/tr//td[text()='Adjusted Amount']")))
    
  • 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