selenium Test case for facebook login and logout

岁酱吖の 提交于 2019-12-22 22:16:32

问题


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


回答1:


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");




    }

}



回答2:


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");

    }



回答3:


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




回答4:


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();   



回答5:


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>



回答6:


It's very easy to solve. Instead of using

click()

action use

submit()

since logout is in the form.




回答7:


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.




回答8:


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);

  }
}



回答9:


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();
    }
}


来源:https://stackoverflow.com/questions/32244865/selenium-test-case-for-facebook-login-and-logout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!