我使用selenium.WebDriver和HtmlUnitDriver登錄到Facebook,但我無法註銷。如何使用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
&& Event.__inlineSubmit &&
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
的[從Facebook中硒的webdriver登出]可能的複製(http://stackoverflow.com/questions/38524957/selenium-webdriver-log-out-from-facebook) –
做什麼你的意思是還沒有工作?你能分享你是如何從建議的鏈接嘗試? –
你確定它拋出'NoSuchElementException',因爲'wait.until(ExpectedConditions.elementToBeClickable(By.linkText(「Log Out」)));'如果條件不符合,會拋出'TimeoutException'?請驗證它。 –