如何使用selenium.WebDriver和HtmlUnitDriver注销的Facebook

问题描述 投票:2回答:5

我使用selenium.WebDriver和HtmlUnitDriver登录到Facebook上,但我不能注销。

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

WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.facebook.com");
System.out.println("Title of the page "+ driver.getTitle());
WebElement username = driver.findElement(By.id("email"));
username.sendKeys("[email protected]"); 
WebElement password = driver.findElement(By.id("pass"));
password.sendKeys("xxxxx");
WebElement Signup_button = driver.findElement(By.id("loginbutton"));
Signup_button.click();
Thread.sleep(5000);
System.out.println("After login title is = " + driver.getTitle());

到现在为止它的工作原理。和下面的步骤覆盖注销部分(不工作):

WebElement logOut = driver.findElement(By.id("userNavigationLabel"));
logOut.click();
Thread.sleep(5000);
WebElement signOut = driver.findElement(By.name("Log Out"));
logOut.click();

System.out.println("Logged Out Successfully!!!!!!!!!!!!!!!!!!!");
String pagetitle = driver.getTitle();
System.out.println(pagetitle);

}}

错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with name: Log Out

Facebook的注销HTML代码

<span><span class="_54nh"><form class="_w0d"
 action="https://www.facebook.com/logout.php" data-nocookies="1" 
id="show_me_how_logout_1" method="post" onsubmit="return window.Event
 &amp;&amp; Event.__inlineSubmit &amp;&amp; 
Event.__inlineSubmit(this,event)"><input name="fb_dtsg" 
value="AQGzvtPjFD1W:AQFslBEMcQG9" autocomplete="off" type="hidden"><input
 autocomplete="off" name="ref" value="mb" type="hidden"><input 
autocomplete="off" name="h" value="AfeziZXPRhN6ncxD" type="hidden">
</form>Log Out</span></span>

我也通过SAURABH野牛Selenium WebDriver Log Out from Facebook尝试这个代码

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement accountSettings = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account Settings")));
accountSettings.click() //this will click on setting link to open menu
WebElement logOut = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log Out")));
logOut.click() // this will click on logout link

错误

Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for element to be clickable: By.linkText: Log Out
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:03'
System info: xxxxxxxxxxx java.version: '1.8.0_101'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:259)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
    at HtmlDemoProgram1.main(HtmlDemoProgram1.java:53)
Caused by: org.openqa.selenium.NoSuchElementException: No link found with text: Log Out
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
java facebook selenium selenium-webdriver
5个回答
1
投票

Facebook的注销

了Thread.sleep(3000);

。driver.findElement(By.xpath( “// DIV [含有(@ ID, 'userNavigationLabel')]”))点击();

了Thread.sleep(3000);

。driver.findElement(By.xpath( “//跨度[@class = '_ 54nh'] [含有( '注销')]”))点击();


0
投票

有一个在名称(定位)没有属性,请使用其他属性(定位)为ID =“show_me_how_logout_1”或任何其他定位。


0
投票

试试下面的代码。

WebElement logOut = driver.findElement(By.id("userNavigationLabel"));
logOut.click();
Thread.sleep(5000);
WebElement signOut = driver.findElement(By.partialLinktext("Log Out"));
signOut.click();

0
投票

下面的代码可以帮助你....谢谢。

包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("[email protected]");

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


}

}


0
投票

公共无效LogOutFB()抛出异常{

    WebElement logOut = driver.findElement(By.id("userNavigationLabel"));
    logOut.click();
    Thread.sleep(5000);
    driver.findElement(By.xpath("//span[@class='_54nh'][contains(.,'Log Out')]")).click();
    System.out.println("Logged Out Successfully!");

}
© www.soinside.com 2019 - 2024. All rights reserved.