1

If you saw scroll spy in facebook logout functionality, I'm not able to find out XPath within that span class, on trying with .Class and .ID and it didn't work for me.

If I want to click on Logout button in Facebook or LinkdIn: For first click, it opens scroll down the functionalities like Myprofile, logout, Home...etc

How to click on that element (Logout button) which is within that scroll spy.

Your answers will be highly appreciated..!

closed as unclear what you're asking by alecxe, Bharat Mane, NarendraC, IAmMilinPatel, ECiurleo Jun 28 '17 at 10:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Do you mean you are using spies? Or do you mean when you use the browser inspect element. – Michael Durrant Jun 26 '17 at 11:29
  • Also - what are you using for class and id ? Please provide the values you use. – Michael Durrant Jun 26 '17 at 11:30
2

Assuming you are talking about the last menu item, "Log out", with the menu already open (so the element is visible to click on):

//a[contains(@data-gt,'menu_logout')]
1

You can use Firefox extension FirePath to find xpath locators on the fly. It's a Firebug extension that adds a development tool to edit, inspect and generate XPath 1.0 expressions, CSS 3 selectors and JQuery selectors.

0

When I visit that page as a logged in user the button is not visible and I need to click an arrow to make it appear

To make the button visible, first I have to click on:

<div class="_5lxt f_click" id="userNavigationLabel">Account Settings</div>

which is visible within

<div class="_6a _6b uiPopover _5v-0 openToggler selected" id="logoutMenu" data-nocookies="1"><button class="hideToggler" type="button"></button><a class="_5lxs _3qct _p" aria-haspopup="true" aria-labelledby="userNavigationLabel" data-gt="{&quot;ref&quot;:&quot;async_menu&quot;,&quot;logout_menu_click&quot;:&quot;async_menu&quot;}" href="https://www.facebook.com/settings?ref=mb&amp;drop" id="pageLoginAnchor" rel="toggle" role="button" aria-expanded="false" aria-controls="BLUE_BAR_ID_DO_NOT_USE"><div class="_5lxt f_click" id="userNavigationLabel">Account Settings</div></a><button class="hideToggler" type="button" style="right: 0px;"></button></div>

For automation I would try using the id of logoutMenu, i.e. click on the css div#logoutMenu

When the menu displays I then have to click on:

<span class="_54nh"><form class="_w0d" 
action="https://www.facebook.com/logout.php?button_name=logout&amp;button_location=settings"
data-nocookies="1" id="show_me_how_logout_1" method="post"
onsubmit="return window.Event &amp;&amp; Event.__inlineSubmit &amp;&amp; Event.__inlineSubmit(this,event)">

For automation I could try using the #show_me_how_logout_1 link although I might need to add an implicit wait for the element to appear. I can test that by first adding a explicit sleep of 3 seconds.

You might need to fiddle a bit with this solution but I think this approach is the one you need to deal with such a page state change.

Of course you should also be extracting all the finder stuff into Page Objects.

  • Hello Michael. I have tried this but it still did not work. I have taken the screenshots. How can I share those with you? I am first clicking the menu by clicking the item with id "UserNavigationLabel". So, the menu is displaying with last item as logout. But, not clickable with the id "#show_me_how_logout_1" – Aalok Jun 26 '17 at 12:23
  • Sounds like some progress. How about adding a 3 second wait and/or then looking for the last lielement. Try targeting ul[role='menu'] li:last – Michael Durrant Jun 26 '17 at 12:39
  • Also consider upvote to keep me interested – Michael Durrant Jun 26 '17 at 12:40
  • So, Michael, adding explicit wait has not been fruitful at all. As I was already displaying the menu containing the logout option, so adding explicit wait was something I was counting on. But that didn't work either. I have screenshots. Let me know how do want them. But all in all, interesting conversation. With Xpath, it is hitting bang on target. I am adding my changed code in my earlier answer.... – Aalok Jun 26 '17 at 13:20
0

Use below Xpath, it should work. And as mentioned in the above post. You can use Firefox extension Firepath in order to locate the elements.:

//span[@class='_54nh'][text()='Log Out']

And here is the complete program from logging in to Facebook and logging out. It works:

package helpOthers;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;
public class Facebook_logout {

    @Test(enabled = true)

    public void logout_test () throws InterruptedException{

        // In the below line, set the properly as per the path of your computer drive.
        System.setProperty("webdriver.gecko.driver","D:\\Selenium\\BrowserDrivers\\geckodriver.exe"); //System.setProperty(mozilla driver,Driverpath);
        WebDriver dr=new FirefoxDriver();
        dr.get("http://www.facebook.com");

        Thread.sleep(3000);
        dr.findElement(By.id("email")).sendKeys(username);  //Enter username in double quotes
        dr.findElement(By.id("pass")).sendKeys(password);   // Enter password in double quotes
        dr.findElement(By.xpath("//input[@type='submit']")).click();
        Thread.sleep(3000);
        dr.findElement(By.id("userNavigationLabel")).click();
        Thread.sleep(2000);;
        dr.findElement(By.xpath("//span[@class='_54nh'][text()='Log Out']")).click();
        Thread.sleep(3000);
        dr.quit();  
    }
}

Played around with the code in order to check if the logout element can be identified with id. Used my explicit wait function to see what happens:

'package helpOthers; import Cricket_commonFunctions.*; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class Facebook_logout { GenericFunctions gf= new GenericFunctions();

@Test(enabled = true)

public void logout_test () throws InterruptedException{

    // In the below line, set the properly as per the path of your computer drive.
    System.setProperty("webdriver.gecko.driver","D:\\Selenium\\BrowserDrivers\\geckodriver.exe"); //System.setProperty(mozilla driver,Driverpath);
    WebDriver dr=new FirefoxDriver();
    dr.get("http://www.facebook.com");

    Thread.sleep(3000);

    dr.findElement(By.id("email")).sendKeys(gf.readConfigFile("FacebookUsername"));
    dr.findElement(By.id("pass")).sendKeys(gf.readConfigFile("FacebookPassword"));
    Thread.sleep(10000);
    dr.findElement(By.xpath("//input[@type='submit']")).click();
    Thread.sleep(6000);
    dr.findElement(By.id("userNavigationLabel")).click();
    Thread.sleep(2000);
    //dr.findElement(By.xpath("//span[@class='_54nh'][text()='Log Out']")).click();
    //WebElement logout =dr.findElement(By.id("show_me_how_logout_1"));
    //Thread.sleep(5000);

    boolean element_exists=gf.ElementexistsById(dr, "show_me_how_logout_1");
    if (element_exists){
    Thread.sleep(5000);
    dr.findElement(By.id("show_me_how_logout_1")).click();
    Thread.sleep(3000);
    dr.quit();  
    }

}}

' Look at the if condition.So, when I executed the program in debug mode, it satisfied the if condition but failed when it tried to click the element found by ID.

  • Yes. Although be aware that the id "userNavigationLabel" exists twice so it might work but not be the best selector possible and give maintenance / failing test issues down the road. – Michael Durrant Jun 26 '17 at 11:57
  • Agreed. An oversight issue. Didn't notice the if there are two same ids. – Aalok Jun 26 '17 at 13:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.