首页 > 解决方案 > 如何通过selenium和java根据html单击“注销”链接

问题描述

这是代码,

<div id="user-tools">
    Welcome
    <strong>Admin</strong>
    /
    <a href="/">View</a>
    /
    <a href="/admin/password_change>Change password</a>
    /
    </a href="/admin/logout/">Log out</a>
</div>

我正在尝试使用这种方式单击“注销”链接,但没有运气。

driver.findElement(By.xpath("//a[@href='/admin/logout/'])".click();

有任何想法吗 ?谢谢。

标签: javaseleniumselenium-webdriverxpathcss-selectors

解决方案


要单击带有文本作为注销的元素,您可以使用以下任一定位器策略

  • 选择器

    "div#user-tools a[href='/admin/logout/']"
    
  • XPath

    "//div[@id='user-tools']//a[@href='/admin/logout/'][contains(.,'Log out')]"
    

推荐阅读