如何使用Selenium和Python单击选项卡元素

时间:2018-08-21 16:29:12

标签: python selenium selenium-webdriver xpath css-selectors

我知道已经有类似的问题,但是这些答案都没有帮助我。

我在以下方面遇到一些问题。我有以下标签:

enter image description here

该特定部分的HTML代码如下:

<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>

我想单击访问控制

这就是我要使用的:

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

显然可以找到这些元素,但是没有单击它。没有任何例外或消息,只是不单击它即可。

我以前在其他链接上使用了这种方法,但没有问题,所以我很确定自己在这里一定做错了。

你能帮忙吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

根据 HTML ,您共享了对文本为访问控制的元素调用click()而不是使用方法presence_of_element_located()需要使用方法 element_to_be_clickable() ,您可以使用以下任一解决方案:

  • 使用LINK_TEXT

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

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

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='Tab1Lnk'][contains(@title,'access control')]"))).click()
    

注意:您必须添加以下导入:

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

答案 1 :(得分:0)

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

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