Java 如何使用 selenium 脚本注销 gmail?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24304427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:24:04  来源:igfitidea点击:

How to logout gmail using selenium script?

javaseleniumautomation

提问by user755806

I am writing selenium script for Gmail login and logout functionality. I am able to successfully login using below code.

我正在为 Gmail 登录和注销功能编写 selenium 脚本。我能够使用以下代码成功登录。

//Open gmail
         driver.get("http://www.gmail.com");

         // Enter userd id
         WebElement element = driver.findElement(By.id("Email"));
         element.sendKeys("[email protected]");

         //wait 5 secs for  userid to be entered
         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

         //Enter Password
         WebElement element1 = driver.findElement(By.id("Passwd"));
         element1.sendKeys("Password");

         //Submit button
         element.submit();

But i could not write a script to logout. Could yu please provide me script for logout?

但是我无法编写脚本来注销。你能提供我退出的脚本吗?

Thanks!

谢谢!

回答by Purus

Try the below code.

试试下面的代码。

driver.findElement(By.className("gb_Ta")).click(); // To click the flyout menu
driver.findElement(By.className("gb_71")).click(); // To click the sign out button

回答by twisted_coder

It is a very bad idea trying to automate Gmail. First of all, it is against Google's policy and when you sign up for Gmail, you accepted terms and conditions that you won't automate. Having said that there are many efficient ways to connect to your Gmail account. One of the approaches is to use IMAP client like IMAP4. Using this, you can connect to your Gmail, go through your inbox, delete messages,sign out etc. By doing this, you don't have to worry about automating UI portion. Also, Google changes its UI frequently just to stop people from automating it, so if you automate Gmail UI, then it might work today but it won't work after a couple of days. Go through this link and you should be able to implement IMAP4 in your tests within few minutes: http://mailsystem.codeplex.com/discussions/269058

尝试自动化 Gmail 是一个非常糟糕的主意。首先,这违反了 Google 的政策,当您注册 Gmail 时,您接受了不会自动执行的条款和条件。话虽如此,有许多有效的方法可以连接到您的 Gmail 帐户。其中一种方法是使用 IMAP 客户端,如 IMAP4。使用它,您可以连接到您的 Gmail、浏览您的收件箱、删除邮件、退出等。通过这样做,您不必担心 UI 部分的自动化。此外,Google 经常更改其 UI 只是为了阻止人们对其进行自动化,因此如果您将 Gmail UI 自动化,那么它今天可能会工作,但几天后将无法工作。通过此链接,您应该能够在几分钟内在您的测试中实现 IMAP4:http: //mailsystem.codeplex.com/discussions/269058

回答by sun

Try the code below driver.findElement(By.xpath("//*[@id='gb']/div[1]/div[1]/div/div[3]/div[1]/a")).click();

试试下面的代码 driver.findElement(By.xpath("///*[@id='gb']/div[1]/div[1]/div/div[3]/div[1]/a") )。点击();

    // click on actual logout button step 2
    driver.findElement(By.id("gb_71")).click();

    //closing the webdriver window after successful completion of the test
    driver.close();

回答by ramananda pati

WebDriverWait wait=new WebDriverWait(driver,50);
WebElement logout=driver.findElement(By.cssSelector("span.gb_4.gbii"));
logout.click();
WebElement signout=driver.findElement(By.id("gb_71"));
signout.click();

回答by Kunwar

After login to gmail, Try this code to logout:-

登录 gmail 后,尝试使用此代码注销:-

driver.findElement(By.xpath("//span[@class='gb_7 gbii']")).click(); driver.findElement(By.id("gb_71")).click();

driver.findElement(By.xpath("//span[@class='gb_7 gbii']")).click(); driver.findElement(By.id("gb_71")).click();

回答by venkat

use xpath 

// Click on the image icon present in the top right navigational Bar
driver.findElement(By.xpath("//div[@class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
//Click on 'Logout' Button
driver.findElement(By.xpath("//*[@id='gb_71']")).click();
//Close the browser.
driver.close();

回答by Arfa

use cssSelector

使用 cssSelector

//Click on the profile image present in the right top corner
driver.findElement(By.cssSelector("span.gb_3a.gbii")).click();
//Click on 'Sign Out' button
driver.findElement(By.id("gb_71")).click();  //Close the browser
window driver.close();

回答by user7216468

use cssselector:

使用cssselector

driver.findElement(By.cssSelector("span.gb_8a.gbii")).click();
driver.findElement(By.id("gb_71")).click();

回答by murugaji

Try this code for sign out the gmail using the selenium webdriver. It's working for me

尝试使用此代码使用 selenium webdriver 注销 gmail。它对我有用

driver.findElement(By.cssSelector(".gb_b.gb_db.gb_R")).click();
Thread.sleep(5000);// Click on the image icon present in the top 

right navigational Bar

右侧导航栏

driver.findElement(By.cssSelector(".gb_Fa.gb_Pe.gb_We.gb_wb")).click();
Thread.sleep(5000); //Signout button