硒Web驱动程序中的Gmail注销功能 [英] Gmail logout functionality in selenium web driver

查看:100
本文介绍了硒Web驱动程序中的Gmail注销功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Selenium WebDrive的初学者,尝试创建用于gmail登录和注销的代码,但无法通过"Id"注销,如果有人建议如何注销它,这对我会有所帮助.以下是我执行的代码,请检查并提出建议:

I am a beginner in selenium webdrive and try to make a code for gmail login and logout but not able to logout via "Id", it would be helpful for me if anyone suggest that how to logout it. Below are the code which I did please check and suggest:

public class seleniumExample {
    public static WebDriver driver;
    public static WebElement element, element1;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        // Intialize chrome driver
        driver = new FirefoxDriver();
        driver.get("http://www.gmail.com");
        element = driver.findElement(By.id("Email"));
        element.sendKeys("xyz@gmail.com");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        element1 = driver.findElement(By.id("Passwd"));
        element1.sendKeys("xyz");
        element.submit();
        driver.findElement(By.id("gbi4m1")).click(); 
        driver.findElement(By.id("gb_71")).click(); 
    }

}

推荐答案

以下ID:gbi4m1不指向任何内容.这导致您的测试用例失败.正如我在Google并不是学习硒的合适场所之前所说的那样,该页面非常复杂,如果您是初学者,则很难处理动态ID.

The following id :gbi4m1 does not point to anything. This is causing your test case to fail. As i said before google is not a right place to learn selenium, the page is complex and dynamic id's are hard to deal with if you are beginner.

通常,在浏览器中,您可以使用开发工具来检查是否可以使用选择器来定位元素.因此,在实际在代码中使用特定选择器之前,您可以验证其是否有效.这样可以节省很多时间.

Normally in browsers you can use development tools to check if a selector can be used to locate an element. So before you actually use a particular selector in your code, you can verify if it works. This will save a lot of time.

使用此功能的另一个优势是检查选择器的唯一性.如果有多个具有相同选择器的元素,则findElement()返回第一个.在这个 选择器将有效但不会唯一的情况,这反过来会导致您的测试用例失败.

One more advantage of using this would be to check the uniqueness of a selector. If there are multiple elements with the same selector,findElement() returns the first one. In this case the selector would be valid but it won't be unique, which in turn causes your test case to fail.

如果选择器是唯一的,则控制台仅显示一个元素.

If a selector is unique, console displays only one element.

让我用一个简单的示例进行说明::

Let me explain with a simple example :

要点击此页面右上方的问问题按钮,可以使用以下选择器.

To click on Ask Question button on the top right of this page, following selector would work.

css = #nav-askquestion

因此,现在您可以使用浏览器的开发工具来确认这一点.

So now you can confirm this by using the dev tools of your browser.

只需在控制台上输入$('selector_to_check'),它就会显示与此选择器相关的所有元素.

Just type $('selector_to_check') on the console and it displays all elements related with this selector.

我使用过Chrome:

I have used Chrome :

如果输入的选择器无效,则会显示一个空数组.

If the selector entered is invalid,an empty array is displayed.

这篇关于硒Web驱动程序中的Gmail注销功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆