0

I am trying to automate a website.I am performing the following steps :

  1. Login
  2. LogOut.

I am able to log in successfully. After the Log in step, the Log Out option is present under a button/tab called as MyAccount. But the Log Out option is displayed when one hovers over the MyAccount button. I am trying to write the Selenium code for Log Out step. But i am consistently getting No Element Found Exception.I am using css Selector .The DOM structure of the Log Out element is :

<li class="_2sYLhZ _2mEF1S" data-reactid="31">
<a class="_1AHrFc _2k0gmP" data-reactid="32" href="/account/?rd=0&link=home_account">My Account</a>
<ul class="_1u5ANM" data-reactid="33">
<li class="_2f5Jjv" data-reactid="34">
<a class="_2k0gmP" data-reactid="35" href="/account/?rd=0&link=home_account">Account</a>
</li>
<li class="_2f5Jjv" data-reactid="36">
<a class="_2k0gmP" data-reactid="37" href="/account/orders?link=home_orders">Orders</a>
</li>
<li class="_2f5Jjv" data-reactid="38">
<a class="_2k0gmP" data-reactid="39" href="/account/wallet?link=home_wallet">Wallet</a>
</li>
<li class="_2f5Jjv" data-reactid="40">
<a class="_2k0gmP" data-reactid="41" href="/wishlist?link=home_wishlist">Wishlist</a>
</li>
<li class="_2f5Jjv" data-reactid="42">
<a class="_2k0gmP" data-reactid="43" href="/account/ebookslibrary">eBooks Library</a>
</li>
<li class="_2f5Jjv" data-reactid="44">
<a class="_2k0gmP" data-reactid="45" href="/profile?link=home_review">Reviews & Ratings</a>
</li>
<li class="_2f5Jjv" data-reactid="46">
<a class="_2k0gmP" data-reactid="47" href="/recommendations?link=home_recommendations">Recommendations</a>
</li>
<li class="_2f5Jjv" data-reactid="48">
<a class="_2k0gmP" data-reactid="49" href="/account/subscriptions?link=home_preferences">Email Preferences</a>
</li>
<li class="_2f5Jjv" data-reactid="50">
<a class="_2k0gmP" data-reactid="51" href="/account/Clickme">Click Me</a>
</li>
<li class="_2f5Jjv" data-reactid="52">
<a class="_2k0gmP" data-reactid="53" href="#">Log Out</a>
</li>
</ul>

Below is the peice of code that i am writing for the clicking the Log Out button.

driver.findElement(By.cssSelector("a[text='Log Out'])")).click();

Could someone please tell me what is the error in the css Selector code that I am using.

Andersson
  • 51,635
  • 17
  • 77
  • 129
A Prad
  • 1
  • 5
  • If you want to click `LogOut` that becomes visible only after hovering over `My Account` you might need to use `Actions`. Check following http://stackoverflow.com/questions/17293914/how-to-perform-mouseover-function-in-selenium-webdriver-using-java – Andersson Feb 25 '17 at 18:47
  • @Andersson , i tried to implement the mouser hover action .It worked . But as mentioned in your reference post, the next element is not getting clicked . The actual issue is after the hovering action , i am unable to find the proper css Selector for the LogOut option because the compiler says that the element is not found or invalid selector.Please let me know how to find the find the proper css Selector for the Log Out button. That's where i am stuck now. – A Prad Feb 25 '17 at 19:37
  • Possible duplicate of [Css Selector for the Log Out button](http://stackoverflow.com/questions/42460815/css-selector-for-the-log-out-button) – JeffC Feb 26 '17 at 00:33

2 Answers2

0

There is no such possibility in css selector to select element by child text node. You might use one of attributes as below:

driver.findElement(By.cssSelector("a[data-reactid='53'])")).click();

or to use search by XPath selector if you still want to use text content as identifier:

driver.findElement(By.xpath("//a[text()='Log Out'])")).click();

or by link text:

driver.findElement(By.linkText("Log Out")).click();
Andersson
  • 51,635
  • 17
  • 77
  • 129
0

Try to move mouse over My Account button using selenium actions like this( Im writing c# code but am sure that methods and classes are the same in java, maybe they differ in case):

Actions action  = new Actions(driver);
action.MoveToElement(myAccountElement).Perform();

Then, use webdriverwait and explicitly wait for some seconds. Afrer waiting, find your element(logout button) and perform click.

But...instead of .click() method, use .sendKeys(keys.Enter) or .sendKeys(keys.Return)

click method sometimes do not work on elements(my personal experience on different sites)

PS: your logout link has href set to '#'. Try this in your cssSelector:

a[href*='#'])

Efe
  • 800
  • 10
  • 32