0

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

4
  • Please add a link to the web page and clarify what element you trying to access there? Commented Jul 29, 2021 at 7:50
  • What do you mean by I'm trying to locate a tab ? - does that mean the element is in new tab or window ? Commented Jul 29, 2021 at 8:14
  • Thank you all for the comments. This is an internal webpage and PSW is required, so I added one screenshot of the page, hope it will help. Commented Jul 30, 2021 at 1:15
  • It's not a new window. It is a tab on the webpage, when I click the tab, part of the webpage will be refreshed and new content will appear. Commented Jul 30, 2021 at 1:18

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer! It works for other elements. However, it does not work on this element, here is the error: selenium.common.exceptions.TimeoutException: Message I think it is because of my wrong Xpath, but I don't what is the correct Xpath
@BigJay This is a great free learning resource to learn how to identify web element: testautomationu.applitools.com/web-element-locator-strategies - invest a few hours and you can do it like a pro
Appreciate it! Thank you so much!

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.