Oxycash
Oxycash

Reputation: 197

Clicking on Javascript tab using Selenium and Python without unique class id or element name

I have this HTML element code which I am currently struggling to figure out to use it for clicking on the tab that says Problem. As the "Problem" doesnt have a unique classname or element ID, I am unable to figure how to send a Click().

I have tried to check if z-index can be used as index(assumed) and used below line of code

browser.switch_to_frame(a[3])

but it seems I am wrong.

HTML code as below

<div class="TabsViewPort" style="position: relative; overflow: hidden; width: 896px; height: 22px; float: left;">
<div style="overflow: visible; float: left; width: 897px; top: 0px; left: 0px;">
<dl class="OuterOuterTab">
<dd class="OuterTab" artabid="955000038" arwindowid="0" style="top: 1px; z-index: 1; left: 0px; visibility: inherit; display: block;"><span class="TabLeftRounded">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1" style="color:#000000;">My&nbsp;Profile</a>
</span>
<span class="TabRight">&nbsp;</span>
</dd>
<dd class="OuterTabSelected" artabid="600000203" arwindowid="0" style="top: 1px; z-index: 3; left: 63px; visibility: inherit; display: block;"><span class="TabLeft">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1">Approval</a>
</span>
<span class="TabRight">&nbsp;</span>
</dd>
<dd class="OuterTab" artabid="536870915" arwindowid="0" style="top: 1px; z-index: 1; left: 409px; visibility: inherit; display: block;"><span class="TabLeft">&nbsp;</span>
<span class="Tab"><a href="javascript:" class="btn f1">Problem</a>
</span>
<span class="TabRight">&nbsp;</span>
</dd>
</dl>
</div>
</div>

Upvotes: 1

Views: 654

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193088

The element with text as Problem is a JavaScript enabled element so to click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • Using XPATH A:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='TabsViewPort']//dl[@class='OuterOuterTab']//dd[@class='OuterTab']//a[@class='btn f1' and text()='Problem']"))).click()
    
  • Using XPATH B (shortened):

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn f1' and text()='Problem']"))).click()
    
  • 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
    

Upvotes: 1

KunduK
KunduK

Reputation: 33384

If the element present inside an iframe then you need to switch to iframe first to access the element. You can Use following method to frame_to_be_available_and_switch_to_it()

By locator ID

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"id of the iframe")))

OR

By locator NAME

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"name of the iframe")))

Once You have switched to iframe you can access the element using following xpath

To click on the element Induce WebDriverWait and element_to_be_clickable()

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[@class='Tab']//a[text()='Problem']"))).click()

You need to import following to execute above code.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Hope this will help.

Upvotes: 1

SeleniumUser
SeleniumUser

Reputation: 4177

please find below xpath to click on third TAB

(//span[@class="Tab"])[3]/a

Upvotes: 0

Related Questions