2013-03-26 47 views
0

您能否让我知道我们如何使用硒注销Chrome浏览器?Facebook登出脚本

e.g

public class AJ { 
    public static void main(String[] args) { 
     WebDriver driver = new ChromeDriver(); 
     driver.get("http://facebook.com"); 
     WebElement element=driver.findElement(By.name("email")); 
     element.sendKeys("[email protected]"); 
     element=driver.findElement(By.name("pass")); 
     element.sendKeys("password"); 

     element.submit(); 

回答

3

下面的代码应该帮助你。

public static void main(String[] args) { 
    WebDriver driver = new ChromeDriver(); 
    driver.get("http://facebook.com"); 
    WebElement element=driver.findElement(By.name("email")); 
    element.sendKeys("[email protected]"); 
    element=driver.findElement(By.name("pass")); 
    element.sendKeys("password"); 

    element.submit(); 

    //Click on dropdown menu then logout button 
    driver.findElement(By.id("userNavigationLabel")).click(); 
    driver.findElement(By.id("logout_form")).click(); 

    //Check to see if email login box is available 
    //therefore confirming user has logged out 
    driver.findElement(By.name("email")); 
} 

我建议您使用Chrome开发者工具来帮助您找到硒找到一个页面的独特属性。

我希望这有助于!

0

在python中,使用这行代码也是一样。它使用相同的模块,即Selenium。 所以,只需使用下面传递的参数使用css选择器来更改元素即可。

logout1 = driver.find_element_by_css_selector("._w0d[action='https://www.facebook.com/logout.php?button_name=logout&button_location=settings']").submit() 

希望它有效。

0

我能够从Facebook成功注销。

这里是Java代码

字符串URL = “http://facebook.com”;

String email = "email"; 
    String password = "password"; 
    System.setProperty("webdriver.chrome.driver", "src/chromedriver 3"); 
    WebDriver driver = new ChromeDriver(); 

    ChromeOptions options = new ChromeOptions(); 
    options.addArguments("--disable-notifications"); 


    driver = new ChromeDriver(options); 

    driver.get(url); 

    driver.manage().window().maximize(); 

    driver.findElement(By.id("email")).sendKeys("Your email here"); 
    driver.findElement(By.id("pass")).sendKeys("Your password here" + Keys.ENTER); 
    driver.findElement(By.id("logoutMenu")).click(); 
    Thread.sleep(2000); 
    driver.findElement(By.xpath("//form[contains(@id,'show_me_how_logout')]/../../../..")).click();