Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
facebook登录和注销的selenium测试用例_Selenium_Selenium Webdriver_Automation_Selenium Ide - Fatal编程技术网

facebook登录和注销的selenium测试用例

facebook登录和注销的selenium测试用例,selenium,selenium-webdriver,automation,selenium-ide,Selenium,Selenium Webdriver,Automation,Selenium Ide,场景是登录到facebook帐户,然后注销。我尝试使用xpath类名和id。但每次它都显示错误为ElementNotfound或element not visible。后来,我使用SELENIUM IDE检查了它,并获得了用于注销链接的xpath。但错误依然存在。请帮帮我 公共类facebook登录{ 公共静态void main(字符串[]args)引发InterruptedException{ //TODO自动生成的方法存根 WebDriver=newfirefoxdriver(); driv

场景是登录到facebook帐户,然后注销。我尝试使用xpath类名和id。但每次它都显示错误为ElementNotfound或element not visible。后来,我使用SELENIUM IDE检查了它,并获得了用于注销链接的xpath。但错误依然存在。请帮帮我

公共类facebook登录{
公共静态void main(字符串[]args)引发InterruptedException{
//TODO自动生成的方法存根
WebDriver=newfirefoxdriver();
driver.manage().window().maximize();
驱动程序。获取(“https://www.facebook.com/");
《睡眠》(2000年);
WebElement用户名=driver.findElement(By.id(“电子邮件”));
WebElement password=driver.findElement(By.id(“pass”);
WebElement Login=driver.findElement(By.id(“u_0_v”));
username.sendKeys(“myusername@xyz.com");
密码。发送密钥(“我的密码”);
Login.click();
《睡眠》(2000年);
//driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
WebElement navigationclick=driver.findElement(By.id(“注销菜单”);
WebElement logout=driver.findElement(By.xpath(“//div[@id='u___1']]/div/div/div/div/div/ul/li[12]/a/span/span”);
导航单击。单击();
if(logout.isEnabled()&&logout.isDisplayed()){
注销。单击();
}
否则{
System.out.println(“未找到元素”);
}
}
}
用于注销的HTML代码 注销

您可以使用JavascriptExecutor在登录到系统后单击注销

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

如果您仍然面临问题,请与我联系

此代码解决了从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();   

如果有人正在寻找Selenium IDE解决方案,或者希望调整上述解决方案,我将使用下面的解决方案,以及goto扩展和FacebookUserName和FacebookPassword的定义值

注意,我首先检查用户是否登录-如果他们登录,我首先注销,如果不是,我只登录

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

打开
https://www.facebook.com
storeElementPresent
id=用户导航标签
洛格丁
戈托夫
“${LoggedIn}”!=假的
洛格丁
点击
id=用户导航标签
点击
//span[text()=“注销”]
标签
洛格丁
类型
id=电子邮件
${FacebookUserName}
类型
id=通过
${FacebookPassword}
点击
//输入[@value='login']
这很容易解决。 而不是使用

单击()

动作使用

submit()


因为注销是在表单中

使用selenium注销facebook的Python代码

driver.find_element_by_css_selector("._w0d[action='https://www.facebook.com/logout.php']").submit()
使用CSS选择器方法,使用类“.\u w0d”和属性for action=”选择注销元素https://www.facebook.com/logout.php".
这是一个表单,应该提交,因此使用“提交”方法。

添加以下简单代码:

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




    }

}
工作:

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

    }

facebook登录和注销代码:(工作)


对于上面的代码,我仍然得到相同的错误。我正在附加html代码“Log Out”,请将您在线程“main”org.openqa.selenium.NoSuchElementException中的consoleException上遇到的错误发送给我:找不到元素:{“method”:“xpath”,“selector”:”//span[包含(,'Log Out')]}命令持续时间或超时:74毫秒正如您在问题中提到的,我在回答中编辑了xpath。请再试一次,让我知道。事实上,facebook的xpath在不断变化,用户对用户对我的xpath是。//*[@id='u_d_7']/div/div/div[1]/div/div/ul/li[18]/a/span/span谢谢你的努力@Shubham Jain,正如你提到的,每次我检查注销时xpath都在变化。我已经发布了我的解决方案,如果有更好的解决方案,学习和理解它将是非常棒的。
.implicitlyWait()
实际上并不等待。。。它只是设置默认的等待。请阅读。请不要忘记在可能的情况下将其标记为答案。xpath中的静态值可能并不总是有效的。在本例中,它使用like
//li[12]
。您可能需要更通用但更独特的
位置标识符
请解释您的解决方案与OPs代码的区别,以及为什么这是解决方案。
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");

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