BigJay
BigJay

Reputation: 3

Python + Selenium: Unable to locate element

I'm trying to locate a tab and click it on my webpage. However, my code does not work:

ActionChains(driver).move_to_element(driver.find_element_by_xpath("//*[@id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div")).perform()
additionaldata_ele= driver.find_element_by_xpath("//*[@id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div").click()

The HTML body is as follows:

<li class="WJX1 WLV1" id="tab_015ba30c-af6c-4c9a-ac34-f77ee00805b6" role="tab" aria-controls="tabPanel_16845ddd-961b-4581-89da-a6a4e6080930" data-automation-id="tab" aria-selected="false"><div class="WGX1"></div><div class="WEX1"><div class="gwt-Label WLX1" data-automation-id="tabLabel">Additional Data</div></div></li>

I get the error -

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div"}

I guess the reason is that when I try to find the element, it doesn't appear on the DOM, so I should implement WebDriverWait until the element is visible. So I tried WebDriverWait, but it didn't work either.

Many thanks for all answers and replies!

second edition: Here is the webpage, sorry I cannot share the link, it is an internal webpage and PSW is required:

This is the screenshot of the page

Upvotes: 0

Views: 170

Answers (1)

RichEdwards
RichEdwards

Reputation: 3743

That id looks dynamic - it will probably change frequently and result in an unstable script.

Additionally, you will want a wait to ensure your web page is ready before you continue.

try something like this:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[text()="Additional Data"]'))).click()

Upvotes: 2

Related Questions