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条回答
小情绪 Triste *
2楼-- · 2019-05-28 13:51

Adding below simple code:

    package open_website;

    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.By;
    import org.openqa.selenium.chrome.ChromeDriver;




        public class Open_website_1
       {

        public static void main(String[] args) {
        // TODO Auto-generated method stub

        String expath = "D:\\Eclipse\\chromedriver_win32\\chromedriver.exe";

        System.setProperty("webdriver.chrome.driver", "D:\\Eclipse\\chromedriver_win32\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();



        driver.get("http:\\www.facebook.com");


        WebElement element1 = driver.findElement(By.id("email"));
        element1.sendKeys("patil_y7@yahoo.com");

        WebElement element2 = driver.findElement(By.id("pass"));
        element2.sendKeys("pa$$word123");

        WebElement element3 = driver.findElement(By.id("u_0_q"));
        element3.click();

        System.out.println("Login");




        WebElement lstitem=driver.findElement(By.id("userNavigationLabel"));
        lstitem.click();

        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);

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

        System.out.println("Log out");




    }

}
查看更多
Juvenile、少年°
3楼-- · 2019-05-28 13:51

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.

<tr>
<td>open</td>
<td>https://www.facebook.com</td>
<td></td>
</tr>
<tr>
<td>storeElementPresent</td>
<td>id=userNavigationLabel</td>
<td>LoggedIn</td>
</tr>
<tr>
<td>gotoIf</td>
<td>'${LoggedIn}' != false</td>
<td>LoggedIn</td>
</tr>
<tr>
<td>click</td>
<td>id=userNavigationLabel</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>//span[text()='Log Out']</td>
<td></td>
</tr>
<tr>
<td>label</td>
<td>LoggedIn</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=email</td>
<td>${FacebookUserName}</td>
</tr>
<tr>
<td>type</td>
<td>id=pass</td>
<td>${FacebookPassword}</td>
</tr>
<tr>
<td>click</td>
<td>//input[@value='Log In']</td>
<td></td>
</tr>
查看更多
何必那么认真
4楼-- · 2019-05-28 13:59

It's very easy to solve. Instead of using

click()

action use

submit()

since logout is in the form.

查看更多
登录 后发表回答