Home > Software design >  I need to click on a td, but selenium reports an error "selenium.common.exceptions.NoSuchElemen
I need to click on a td, but selenium reports an error "selenium.common.exceptions.NoSuchElemen

Time:09-25

This is the HTML of the page:

<td class="titulo_lateral" onclick="javascript: abreMenu(&quot;layer8&quot;);" style="cursor:pointer;">RELATÓRIOS</td>

I'm trying this:

driver.find_element_by_xpath("//*[@id='f']/table/tbody/tr/td/table[2]/tbody/tr/td")

CodePudding user response:

Are you sure that the td, and thus the corresponding XPath, has been rendered on the page when that line of Selenium has executed?

If so, you can try using the full XPath rather than the relative XPath Copy Full XPath button pictured here

CodePudding user response:

You can try with below xpath.

//td[@class='titulo_lateral']

or

//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]

but first you will have to make sure that if it is unique in HTMLDOM or not.

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

once we have unique matching xpath. you can try to click on it by below methodology.

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//td[text()='RELATÓRIOS' and contains(@onclick,'abreMenu')]")
ActionChains(driver).move_to_element(button).click().perform()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
  • Related