简体   繁体   中英

How to click on a tab element using Selenium & Python

I know there are similar questions already but none of those answers helped me.

I'm having some issues with the following. I've got the following tabs:

在此处输入图片说明

The HTML code for that particular section looks like this:

<td class="Tab1TblSelTd"><div class="Tab1SelTxtNew" title="Current Selection: Common Tasks">Common Tasks</div></td>
<td><a href="../task/Home?Home.tabCommon.TabHref=1&amp;jato.pageSession=AKztAAVzcgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAMdwgAAAAQAAAABHQADGN1cnJlbnRSZWFsbXQAAS90ABRDQ1RhYnMuU2VsZWN0ZWRUYWJJZHQAATB0ABVvcGVuc3NvLlNlbGVjdGVkVGFiSWR0AAEwdAASQ3VycmVudFByb2ZpbGVWaWV3dAABL3g$" name="Home.tabCommon.TabHref" class="Tab1Lnk" title="Click to configure access control" onmouseover="window.status='Click to configure access control'; return true" onmouseout="window.status=''; return true" onblur="window.status=''; return true" onfocus="window.status='Click to configure access control'; return true">Access Control</a></td>
<td><a href="../task/Home?Home.tabCommon.TabHref=2&amp;jato.pageSession=AKztAAVzcgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAMdwgAAAAQAAAABHQADGN1cnJlbnRSZWFsbXQAAS90ABRDQ1RhYnMuU2VsZWN0ZWRUYWJJZHQAATB0ABVvcGVuc3NvLlNlbGVjdGVkVGFiSWR0AAEwdAASQ3VycmVudFByb2ZpbGVWaWV3dAABL3g$" name="Home.tabCommon.TabHref" class="Tab1Lnk" title="Click to go to Federation" onmouseover="window.status='Click to go to Federation'; return true" onmouseout="window.status=''; return true" onblur="window.status=''; return true" onfocus="window.status='Click to go to Federation'; return true">Federation</a></td>
<td><a href="../task/Home?Home.tabCommon.TabHref=4&amp;jato.pageSession=AKztAAVzcgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAMdwgAAAAQAAAABHQADGN1cnJlbnRSZWFsbXQAAS90ABRDQ1RhYnMuU2VsZWN0ZWRUYWJJZHQAATB0ABVvcGVuc3NvLlNlbGVjdGVkVGFiSWR0AAEwdAASQ3VycmVudFByb2ZpbGVWaWV3dAABL3g$" name="Home.tabCommon.TabHref" class="Tab1Lnk" title="Click to go to Configuration" onmouseover="window.status='Click to go to Configuration'; return true" onmouseout="window.status=''; return true" onblur="window.status=''; return true" onfocus="window.status='Click to go to Configuration'; return true">Configuration</a></td>
<td><a href="../task/Home?Home.tabCommon.TabHref=5&amp;jato.pageSession=AKztAAVzcgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAMdwgAAAAQAAAABHQADGN1cnJlbnRSZWFsbXQAAS90ABRDQ1RhYnMuU2VsZWN0ZWRUYWJJZHQAATB0ABVvcGVuc3NvLlNlbGVjdGVkVGFiSWR0AAEwdAASQ3VycmVudFByb2ZpbGVWaWV3dAABL3g$" name="Home.tabCommon.TabHref" class="Tab1Lnk" title="Click to go to Sessions" onmouseover="window.status='Click to go to Sessions'; return true" onmouseout="window.status=''; return true" onblur="window.status=''; return true" onfocus="window.status='Click to go to Sessions'; return true">Sessions</a></td>
</tr>
</table>

And I want to click on Access Control .

This is what I'm trying to use:

wait.until(ec.presence_of_element_located((By.LINK_TEXT, 'Access Control'))).click()

Which apparently finds the elemen OK, but it doesn't click on it. There are no exceptions or messages of any kind, it simply doesn't click it.

I've used this approach with other links before, and it worked no problem, so I'm pretty sure I must be doing something wrong here.

Can you help?

Thanks!

As per the HTML you have shared to invoke click() on the element with text as Access Control instead of using the method presence_of_element_located() you need to use the method element_to_be_clickable() and you can use either of the following solution:

  • Using LINK_TEXT :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Access Control"))).click()
  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.Tab1Lnk[title$='access control']"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='Tab1Lnk'][contains(@title,'access control')]"))).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

尝试单击父 td 元素而不是 xpath:

//td[./a[.='Access Control']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM