suffa suffa - 4 months ago 60
Java Question

How to resolve logging out of Facebook with Selenium using Java

I have searched high and low on how to logout of Facebook with Selenium. I've pored through questions on StackOverflow, and many other sites that cover this subject using Selenium, although unsuccessful. I am learning Selenium, and thought it to be pretty straightforward until faced with this hurdle. No matter what I do, Selenium will not logout of Facebook. Below is the code that I'm using. I appreciate any direction (I used Firepath to get the xpath address):

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.BeforeSuite;
import org.testng.annotations.Test;

public class GetFacebookTitle {

public String bu="https://www.facebook.com";
public WebDriver driver;



@BeforeSuite
public void betest(){
driver=new FirefoxDriver();
driver.get(bu);
}

@Test(priority=2)
public void test2()
{
String tilte=driver.getTitle();
System.out.println(tilte);
}

@Test(priority=1)
public void test1()
{
driver.get(bu);
driver.findElement(By.id("email")).sendKeys("name@hotmail.com");
driver.findElement(By.id("pass")).sendKeys("mypassword");
driver.findElement(By.id("loginbutton")).submit();
//System.out.println(title);
}

@Test(priority=3)
public void test3()
{
WebElement lstitem=driver.findElement(By.id("userNavigationLabel"));
lstitem.click();
lstitem.findElement(By.xpath("html/body/div[17]/div/div/div/div/div[1]/div/div/ul/li[16]/a/span/span")).click(); //This line just does not work xpath


}


}

Here is the error that is returned:

Facebook
PASSED: test1
PASSED: test2
FAILED: test3
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/div[17]/div/div/div/div/div[1]/div/div/ul/li[16]/a/span/span"}
Command duration or timeout: 33 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'RLaptop', ip: '192.168.xx.x', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_73'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: c5781a63-5d20-4a55-9cd6-7cd70aa38165
*** Element info: {Using=xpath, value=html/body/div[17]/div/div/div/div/div[1]/div/div/ul/li[16]/a/span/span}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:198)
at org.openqa.selenium.remote.RemoteWebElement.findElementByXPath(RemoteWebElement.java:295)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:194)
at Testing.GetFacebookTitle.test3(GetFacebookTitle.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/div[17]/div/div/div/div/div[1]/div/div/u l/li[16]/a/span/span"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'RLaptop', ip: '192.168.xx.x', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_73'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Rozelle/App Data/Local/Temp/anonymous1905752364377844577webdriver- profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10723)
at <anonymous class>.FirefoxDriver.prototype.findChildElement(file:///C:/Users/Rozelle/AppData /Local/Temp/anonymous1905752364377844577webdriver- profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10735)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Rozelle/AppD ata/Local/Temp/anonymous1905752364377844577webdriver- profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12614)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/Rozelle/AppDat a/Local/Temp/anonymous1905752364377844577webdriver- profile/extensions/fxdriver@googlecode.com/components/command- processor.js:12619)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/Rozelle/AppData/Local/Temp/anonymous1905752364377844577webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12561)

Answer

Instead of this line

lstitem.findElement(By.xpath("html/body/div[17]/div/div/div/div/div[1]/div/div/ul/li[16]/a/span/span")).click();

use the below line and it should work.

driver.findElement(By.partialLinkText("Log Out")).click();