-1

Here is the button I am attempting to click on

<a href="/logout/?t=1550846736%2C09865a11c32ef819fb524c408c8f36cc" class="menu-linkRow">Log out</a>

Here is what I have tried

driver.findElement(By.xpath("//a[contains(@class,'menu-linkRow')]")).click();
driver.findElement(By.xpath("//a[@href='/logout/?t=1550846736%2C09865a11c32ef819fb524c408c8f36cc']")).click();
Scrub
  • 33
  • 5

3 Answers3

1

You can try,

driver.findElement(by.linkText("Log out")).click();

It would be clear if give more details, like the exception you are getting and more!

Cheers!

dheeraj
  • 324
  • 1
  • 9
0

Usually locating elements by xpath is a bad idea.

Try other approaches such as:

  • Locating by CSS Selector (should be your FIRST approach everytime) (This little guide will help you understand them). This includes the ability to specify patterns on element attributes such as:
[attribute~=value]  [title~=flower] Selects all elements with a title attribute containing the word "flower"
  • Locating by any other strategy EXCEPT xpath
  • Locating by xpath as the very last resort.

Locating by xpath is considered an expensive operation and is extremely difficult to mantain.

You can also use whatever strategy you like but getting a collection of elements and later filtering them out by means of your favourite programming technique (i.e using Java8 Streams api), o just running another element search inside your elements such as:

element.findBy...

I strongly recommend adopting css selectors, as they are being heavily used to add style to any modern web application. So if the developer managed to resolve styling with css selectors, you will also be able to.

Gaston Martin
  • 11
  • 2
  • 5
  • One last comment: Usually there are two problems when locating elements: you got no matches you got too many matches If your are hitting the second kind of problem, please post the whole DOM as a gist so we can better help you understanding what is going on. – Gaston Martin Feb 22 '19 at 15:25
0

driver.findElement(by.Css('a.menu-linkRow')).click();

Also your second sentence "//a[@href='/logout/?t=1550846736%2C09865a11c32ef819fb524c408c8f36cc']" is using a session based locator which will not work on another session.

There is not need to use text and xpath as will be slower than css.

teddym6
  • 88
  • 9