简体   繁体   English

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

[英]How to logout of Facebook using selenium.WebDriver and HtmlUnitDriver

I am using selenium.WebDriver and HtmlUnitDriver to login to Facebook, but I can't logout.我正在使用 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("xxxxx@gmail.com"); 
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());

Until now it works.到现在为止它有效。 And The following steps cover the logout part (not working):并且以下步骤涵盖了注销部分(不起作用):

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

}}

Error:错误:

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

Facebook Logout html code 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>

I have also tried this Code by Saurabh Gaur Selenium WebDriver Log Out from Facebook我也尝试过 Saurabh Gaur Selenium WebDriver 从 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

Error错误

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

Facebook Logout Facebook 注销

Thread.sleep(3000);线程.sleep(3000);

driver.findElement(By.xpath("//div[contains(@id,'userNavigationLabel')]")).click(); driver.findElement(By.xpath("//div[contains(@id,'userNavigationLabel')]")).click();

Thread.sleep(3000);线程.sleep(3000);

driver.findElement(By.xpath("//span[@class='_54nh'][contains(.,'Log Out')]")).click(); driver.findElement(By.xpath("//span[@class='_54nh'][contains(.,'Log Out')]")).click();

There is no attribute in name(locator), please use another attribute(locator) as id="show_me_how_logout_1" or any other locator.名称(定位器)中没有属性,请使用另一个属性(定位器)作为 id="show_me_how_logout_1" 或任何其他定位器。

Try the below code.试试下面的代码。

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

Below code may help you.... Thanks.下面的代码可以帮助你....谢谢。

package open_website; 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 void LogOutFB()throws Exception{公共无效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!");

}

Thread.sleep(10000);线程.sleep(10000); driver.findElement(By.cssSelector("div[aria-label='आपकी प्रोफ़ाइल']>svg>path")).click(); driver.findElement(By.cssSelector("div[aria-label='आपकीप्रोफ़ाइल']>svg>path")).click(); Thread.sleep(10000);线程.sleep(10000); driver.findElement(By.xpath("//*[contains(text(),'लॉग आउट करें')][1]")).click(); driver.findElement(By.xpath("//*[contains(text(),'लॉग आउट करें')][1]")).click(); System.out.println("Loggout successfuly from Mayank fb account"); System.out.println("从 Mayank fb 账号注销成功");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium WebDriver处理HtmlUnitDriver的身份验证? - How do I handle authentication with the HtmlUnitDriver using Selenium WebDriver? 如何通过Selenium.WebDriver启动没有安全模式的Firefox - How to start Firefox without safe mode via Selenium.WebDriver 如何使用selenium webdriver和浏览器HtmlunitDriver for Java设置代理? - How can i set proxy using selenium webdriver with browser HtmlunitDriver for Java? 无法使用HtmlUnitDriver进行screenShot [Selenium WebDriver java] - Not able to take screenShot using HtmlUnitDriver [Selenium WebDriver java] 使用Selenium HtmlUnitDriver-headless webdriver上传文件 - File Upload using Selenium HtmlUnitDriver-headless webdriver 如何使用Java使用Selenium Webdriver单击此注销图标 - How to click this Logout Icon using Selenium Webdriver using Java 使用Selenium WebDriver的Amazon登录和注销方案 - Amazon login and logout scenario using selenium webdriver 无法在没有UI的框中以Java启动selenium.WebDriver firefox - cannot start selenium.WebDriver firefox in java in a box with no UI 如何使用 HtmlUnitDriver 和 Selenium Java 发送登录凭据 - How to send the login credentials using HtmlUnitDriver and Selenium Java 如何使用Selenium Webdriver + Java在Facebook上发布帖子? - How to make post on Facebook using Selenium Webdriver + Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM