简体   繁体   English

无法在selenium webdriver中找到元素(用户名和密码)

[英]Unable to locate element (username and password) in selenium webdriver

I can't seem to figure this out. 我似乎无法弄清楚这一点。 I have tried using xpath, cssselector and executeScript but no luck. 我尝试过使用xpath,cssselector和executeScript但没有运气。 I am trying to access username and password but my code can't find it. 我试图访问用户名和密码,但我的代码找不到它。

I am new to webdriver and jquery 我是webdriver和jquery的新手

subset of things i've tried. 我试过的事情的一部分。 All of these locate 'login' but not username or password 所有这些都找到'登录'但不是用户名或密码

WebElement username = driver.findElement("username')
WebElement username = driver.findElement(By.xpath("//div[@class='forms']//descendant::input[@name='username']"));
js.executeScript("$('div.input').get(0)");

I have tried several regular expressions in xpath, cssselector. 我在xpath,cssselector中尝试了几个正则表达式。

<div class="forms">
    <form name="formLogin" method="post" action="">
        <div>Please sign in:</div>
        <div style="height:5px"> </div>
        <ul>
            <li class="nameField">
                <div>
                    <input class="hint_color" type="text" style="display: none;" value="" tabindex="1">
                    <input id="username" type="text" tabindex="1" title="USER.NAME" value="username"       name="username" autocomplete="off">
                </div>
                <div style="height:5px;"> </div>
                <div>
            </li>
            <li class="pwdField">
                <div>
                    <input class="hint_color" type="text" style="display: none;" value="" tabindex="2">
                    <input id="username" type="password" title="PASSWORD" tabindex="2" value="password" name="username" autocomplete="off">
                </div>
                <div style="height:5px"> </div>
                <div>
                    <div style="height:5px"> </div>
                    <a href="">Forgot Username/Password</a>
                </div>
            </li>
            <li class="submitField">
                <input id="login" type="submit" value="Log In" name="login">
            </li>
        </ul>
    </form>

Your help will be appreciated. 我们将不胜感激。

In your HTML code you have two tags id=username and two tags name=username. 在您的HTML代码中,您有两个标签id = username和两个标签name = username。 That's wrong and webdriver can't distinguish between those elements. 这是错误的,webdriver无法区分这些元素。

So if you can, please change those ids (id=username // id=password), but if you can't do that, with Selenium IDE record the script and try to find those elements with XPATH. 所以,如果可以,请更改这些ID(id =用户名// id =密码),但如果你不能这样做,Selenium IDE会记录脚本并尝试使用XPATH查找这些元素。

Sorry my english. 对不起我的英文。

pls try by changing the xpath as 请尝试将xpath更改为

Option:1 //div[@class='forms']//descendant::input[@value='username']

Option 2: //input[@value='username']

To be Handy on Locators, their types and how to pick same locator in different ways, you should use initially Selenium Builder or Selenium IDE (firefox Addon) 要使用定位器,它们的类型以及如何以不同方式选择相同的定位器,您应该首先使用Selenium BuilderSelenium IDE (firefox Addon)

record the event you want, change the locator using available ways under target dropdown. 记录您想要的事件,使用目标下拉列表下的可用方式更改定位器。 This will help you to find the exact locator. 这将帮助您找到确切的定位器。 I hope by this way, you can figure out what you are missing. 我希望通过这种方式,你可以弄清楚你错过了什么。

What is this? 这是什么? For both fields ( username and password ), elements id and name is username only. 对于这两个字段( 用户名密码 ),元素idname仅为username If that is wrong you can try the following command: 如果这是错误的,您可以尝试以下命令:

WebElement username = driver.findElement(By.id("username"));

Whatever action you want to perform you can do something like this: 无论您想要执行什么操作,您都可以执行以下操作:

username.click();

or 要么

username.sendKeys("abc@gmail.com");

There are no attributes of the username and password elements themselves which you use to distinguish them, because they have the same id and the same class. 用户名和密码元素本身没有用于区分它们的属性,因为它们具有相同的ID和相同的类。 That's bad html coding; 这是糟糕的HTML编码; but even so, it is possible to work around this because the li elements have different classes nameField and pwdField . 但即便如此,也可以解决这个问题,因为li元素有不同的类nameFieldpwdField The most concise and efficient way is to use CSS selectors: 最简洁有效的方法是使用CSS选择器:

li.nameField input#username
li.pwdField input#username

Using these locators, the webdriver code is: 使用这些定位器,webdriver代码是:

WebElement username = driver.findElement(By.cssSelector("li.nameField input#username"));
WebElement password = driver.findElement(By.cssSelector("li.pwdField input#username"));

Or if you prefer, you can use XPath equivalents: 或者如果您愿意,可以使用XPath等价物:

.//li[@class='nameField']//input[@id='username']
.//li[@class='pwdField']//input[@id='username']

PS All locators verified using Firepath in firefox. PS所有定位器在firefox中使用Firepath验证。

The way, you have written is wrong. 你写的方式是错的。

WebElement username = driver.findElement("username')

Instead of that try this: 而不是尝试这个:

WebElement username = driver.findElement(By.cssSelector(".nameField>div>input[id='username']"));
username.sendKeys("abcdefg");

You can write more than one CssSelector for "username" locator. 您可以为"username"定位器编写多个CssSelector Once you are done with your xpath or CssSelector , please cross check your locators with FirePath . 完成xpath或CssSelector ,请使用FirePath交叉检查定位器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM