2015-08-27 47 views
2

情景是登錄到Facebook帳戶,然後註銷。我嘗試使用xpath className和id。但每次它顯示錯誤爲ElementNotfound或元素不可見。之後,我使用SELENIUM IDE對其進行了檢查,並獲得了此LOGOUT鏈接的xpath。但錯誤依然存在。請幫助我。selenium Facebook登錄和註銷的測試用例

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("[email protected]"); 
    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代碼註銷 註銷

回答

0

此代碼解決了來自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(); 
+0

'.implicitlyWait()'實際上並沒有等待...它只是設置默認的等待。請參閱[的文檔(https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html)。 – JeffC

+0

請不要忘記將它標記爲答案。 – JeffC

+0

在xpath中有靜態值可能並不總是有效。在這種情況下,它使用像'// li [12]'。您可能需要一些更通用但唯一的位置標識符 – Rao

0

您可以使用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); 

你回我,如果你仍然面臨的問題

+0

我仍然得到同樣的錯誤上面的代碼。我附上html代碼'

註銷 「 – vinay

+0

可以,請給我你真讓控制檯 –

+0

異常錯誤在線程「main」org.openqa.selenium.NoSuchElementException:無法找到元素:{「method」:「xpath」,「selector」:「// span [contains(。,'Log Out')]」} 命令持續時間或超時:74毫秒 – vinay

0

如果有人正在尋找硒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> 
-1
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); 

    } 
} 
0

這是很容易解決。 代替使用

click()

動作使用

submit()

因爲註銷的是在形式。

0
用於使用實註銷硒

driver.find_element_by_css_selector("._w0d[action='https://www.facebook.com/logout.php']").submit() 

使用CSS選擇方法

Python代碼,使用類「._w0d」選擇註銷元素和屬性的行動=「https://www.facebook.com /logout.php」。 這是一個表格,應該提交,因此使用「提交」方法。

1

添加下面簡單的代碼:

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




    } 

} 
1

工作:

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

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

請解釋您的解決方案與OPs代碼的不同之處,以及爲什麼這是解決方案。 – TobiMcNamobi