0

Trying to get an element (text) from a table using XPATH and then print it, but got the following error:

NoSuchElementException: Message: no such element: Unable to locate element.

I have waited time but got the same error. How can I get the text?

I've used the following code:

account = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ctl00_lc_ucLeftMenu_li_1_4"]/a[2]')))
account.click()
time.sleep(3)
portfolio = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ctl00_lc_ucLeftMenu_leaf_2_35"]')))
portfolio.click()
time.sleep(3)
sold = driver.find_element_by_xpath('//*[@id="37ef7b7a-3a62-4d56-a479-29c99031de7e"]/table/tbody/tr[8]/td[5]')
print('The amount is: {}'.format(sold.text))
sold1 = float(sold.text)

Please refer to the attached file- looking to get the highlighted text.

enter image description here

6
  • Your xpath is going for the 5th not 10th td, but that is not the point for the error - Could you improve your question, please and provide some code as text not image. Commented Dec 27, 2020 at 20:45
  • The image is the html from which I am trying to get the text. You are right, the highlighted one is the 10th td but, nevertheless , I should have had 612, instead I got the error 'Element not found'. Commented Dec 27, 2020 at 20:55
  • That id might be dynamic and might switch. Commented Dec 27, 2020 at 21:01
  • @Mardjacku That is clear, but again -> Could you improve your question, please and provide some of your written code as text not image. How to create a Minimal, Reproducible Example This would help all of us to reproduce the behavior. Many thanks. Commented Dec 27, 2020 at 21:01
  • @Arundeep Chohan: the id is not dynamic; after clicking the portfolio, the table and the amount that I am trying to get is as shown in the above picture. Commented Dec 27, 2020 at 21:29

1 Answer 1

0

To get that particular text, the xpath was not the correct one. Instead, I've used the following one:

//table/tbody/tr[8]/td[10]

the full code will look like this:

account = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*    
[@id="ctl00_lc_ucLeftMenu_li_1_4"]/a[2]')))
account.click()
time.sleep(3)
portfolio = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//* 
[@id="ctl00_lc_ucLeftMenu_leaf_2_35"]')))
portfolio.click()
time.sleep(3)
sold = driver.find_element_by_xpath('//table/tbody/tr[8]/td[5]')
print('The amount is: {}'.format(sold.text))
sold1 = float(sold.text)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.