-1

Here is the code,

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

And I am trying to use this way to click the "Log out" link, but no luck.

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

Any ideas ? Thanks.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
user3595231
  • 489
  • 3
  • 15
  • Try using `driver.findElement(By.linkText("Log out")).click();` – Mangohero1 Jun 08 '18 at 18:59
  • 2
    What do you mean by *"failed"*? Why opening anchor tag is actually closing tag? – Andersson Jun 08 '18 at 19:19
  • 1
    It looks like your statement is mal-formed. You are missing an ending parenthesis and the double quotes wraping the XPath need to be inside the first ending parens: `driver.findElement(By.xpath("//a[@href='/admin/logout/']")).click();` – Mads Hansen Jun 08 '18 at 19:22
  • Please share what error you are getting, error stack flow – Ishita Shah Jun 09 '18 at 06:02

1 Answers1

1

To click on the element with text as Log out you can use either of the following Locator Strategies:

  • CssSelector:

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

    "//div[@id='user-tools']//a[@href='/admin/logout/'][contains(.,'Log out')]"
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217