selenium Test case for facebook login and logout

2019-05-28 13:17发布

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

9条回答
欢心
2楼-- · 2019-05-28 13:36

This code solved the issue for Logout from Facebook:

driver.findElement(By.xpath("//div[@id='userNavigationLabel']")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//li[12]/a/span/span")).click();   
查看更多
疯言疯语
3楼-- · 2019-05-28 13:37

WORKING:

public class FacebookLogin {

    public static void main(String[] args) throws InterruptedException 

    {
        String exePath = "C:\\\\Users\\Diganta\\Desktop\\Workspace\\Library\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", exePath);

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.facebook.com");
        System.out.println("Successfully opened the website");
        driver.manage().window().maximize();
        driver.findElement(By.id("email")).sendKeys("Enter the USERNAME");
        driver.findElement(By.id("pass")).sendKeys("Enter the PASSWORD");
        driver.findElement(By.id("u_0_r")).click();
        System.out.println("Successfully logged in");
        Thread.sleep(3000);
        driver.findElement(By.id("userNavigationLabel")).click();
        Thread.sleep(2000);
        driver.findElement(By.partialLinkText("Log out")).click();
        System.out.println("Successfully logged out");

    }
查看更多
看我几分像从前
4楼-- · 2019-05-28 13:39

You can use JavascriptExecutor to click on logout after login to system

WebElement logout=driver.findElement(By.xpath("//div[@id='u_d_1']/div/div/div/div/div/ul/li[12]/a/span/span"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", logout);

Get back to me if you are still facing problem

查看更多
啃猪蹄的小仙女
5楼-- · 2019-05-28 13:39

Python code for facebook logout using selenium

driver.find_element_by_css_selector("._w0d[action='https://www.facebook.com/logout.php']").submit()

Using the CSS selector method, select the logout element using the class "._w0d" and attribute for action="https://www.facebook.com/logout.php". This being a form, should be submitted, thus use the "submit" method.

查看更多
Rolldiameter
6楼-- · 2019-05-28 13:47
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FbLoginLogout {

 public static void main(String [] args) throws InterruptedException

 {
     WebDriver driver = new FirefoxDriver();
     driver.get("https://www.facebook.com/login.php");
     driver.manage().window().maximize();
     driver.findElement(By.id("email")).sendKeys("************");
     WebElement pass = driver.findElement(By.id("pass"));
     pass.clear();
     pass.sendKeys("*********");
     pass.submit();
     System.out.println("Logged in Successfully.......");
     //Logout
     driver.findElement(By.id("userNavigationLabel")).click();
     Thread.sleep(5000);
     driver.findElement(By.linkText("Log Out")).click();
     System.out.println("Logged Out Successfully!!!!!!!!!!!!!!!!!!!");
     String pagetitle = driver.getTitle();
     System.out.println(pagetitle);

  }
}
查看更多
太酷不给撩
7楼-- · 2019-05-28 13:47

code for facebook loging and logout : (working)

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class my_DemoTest {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.gecko.driver", "Driver Path\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.facebook.com/");
        driver.findElement(By.id("email")).sendKeys("abcd@gmail.com");
        driver.findElement(By.id("pass")).sendKeys("abcd_password");
        driver.findElement(By.id("loginbutton")).click();
        System.out.println("Login");
        Thread.sleep(6000);
        driver.findElement(By.id("userNavigationLabel")).click();
        Thread.sleep(4000);
        driver.findElement(By.linkText("Log out")).click();
        System.out.println("Logout successfully");
        Thread.sleep(2000);
        driver.quit();
    }
}
查看更多
登录 后发表回答