Scenario is to Login to facebook account and then logout. I tried using xpath className and id. But everytime it shows error as ElementNotfound or element not visible. Later I Checked it using SELENIUM IDE and got this xpath for the LOGOUT link. But still the error persists. Kindly help me.
public class FacebookLogin {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
Thread.sleep(2000);
WebElement username = driver.findElement(By.id("email"));
WebElement password = driver.findElement(By.id("pass"));
WebElement Login = driver.findElement(By.id("u_0_v"));
username.sendKeys("myusername@xyz.com");
password.sendKeys("mypassword");
Login.click();
Thread.sleep(2000);
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement navigationclick = driver.findElement(By.id("logoutMenu"));
WebElement logout = driver.findElement(By.xpath("//div[@id='u_d_1']/div/div/div/div/div/ul/li[12]/a/span/span"));
navigationclick.click();
if(logout.isEnabled() && logout.isDisplayed()) {
logout.click();
}
else {
System.out.println("Element not found");
}
}
}
HTML CODE FOR LOG OUT Log Out
Adding below simple code:
In case anyone is looking for a Selenium IDE solution, or wishes to tweak the above solution here is what I use, along with the goto extension and defined values for FacebookUserName and FacebookPassword
Notice that I first do a check if the user is logged in - if they are logged in I logout first, if not I login only.
It's very easy to solve. Instead of using
click()
action use
submit()
since logout is in the form.