Java 如果密码样式为 display:none,如何使用 Selenium webdriver 输入密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28176498/
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
How to enter password using Selenium webdriver if the password style is display:none
提问by nicolas
I have a page with login and password (betmarathon[d.o.t]com). I want to login to the site using selenium webdriver. Selenium enters the login correctly, but i have problems with entering password. I get "Element not visible" exception.
我有一个带有登录名和密码的页面(betmarathon[dot]com)。我想使用 selenium webdriver 登录到该站点。Selenium 正确输入登录名,但我输入密码时遇到问题。我收到“元素不可见”异常。
My code for selenium looks like this:
我的硒代码如下所示:
driver.findElement(By.id("auth_login")).sendKeys("MY-USERNAME");
driver.findElement(By.id("auth_login_password")).sendKeys("MY-PASSWORD");
Html code of the page looks like this:
页面的 Html 代码如下所示:
<div class="user">
<input id="auth_login" class="empty" type="text" maxlength="40" rel="Login:" name="login" tabindex="1">
</div>
<div class="pass">
<input id="auth_login_password" type="password" regex="^.{6,}$" maxlength="100" rel="Password:" name="login_password" tabindex="2" style="display: none;">
<input class="undefined empty" type="text" value="Password:" tabindex="2" style="display: inline;">
</div>
You can see that there are 2 inputs for password, the first one is not visible and the second is visible. I should enter the password in the first input. After I click on the box manually, the html code changes and the first input for password becomes visible (display:inline) and the second changes to display:none. But how can I do it with selenium webdriver?
您可以看到密码有 2 个输入,第一个不可见,第二个可见。我应该在第一次输入中输入密码。在我手动单击该框后,html 代码发生变化,密码的第一个输入变为可见(display:inline),第二个更改为 display:none。但是我怎么能用 selenium webdriver 做到这一点呢?
Thanks a lot in advance.
非常感谢。
回答by alecxe
Click the second password input
and then send keys to the first one:
单击第二个密码input
,然后将密钥发送到第一个:
driver.findElement(By.xpath("//div[@class='pass']/input[last()]")).click();
driver.findElement(By.id("auth_login_password")).sendKeys("MY-PASSWORD");
回答by Saifur
Possible answer could be javascript
as well. You can directly execute javascript
on a hidden element and set the attribute.
可能的答案也可能是javascript
。您可以直接javascript
在隐藏元素上执行并设置属性。
WebDriver driver;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('auth_login_password').setAttribute('value', val );");
回答by Kalyan Saripalli
driver.ExecuteScript(string.Format("document.getElementById('cred-password-inputtext').value='{0}';",password));
This solved the problem for me
这为我解决了问题