我有一个带有登录名和密码的页面(betmarathon[d.o.t]com)。我想使用硒网络驱动程序登录该网站。硒正确输入登录,但我在输入密码时遇到问题。我收到"元素不可见"异常。
我的硒代码如下所示:
driver.findElement(By.id("auth_login")).sendKeys("MY-USERNAME");
driver.findElement(By.id("auth_login_password")).sendKeys("MY-PASSWORD");
页面的 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>
您可以看到密码有 2 个输入,第一个不可见,第二个可见。我应该在第一个输入中输入密码。手动单击该框后,html代码会更改,并且密码的第一个输入变得可见(display:inline),第二个更改为display:none。但是我如何使用硒网络驱动程序来做到这一点?
提前非常感谢。
单击第二个密码input
,然后将密钥发送到第一个密码:
driver.findElement(By.xpath("//div[@class='pass']/input[last()]")).click();
driver.findElement(By.id("auth_login_password")).sendKeys("MY-PASSWORD");
可能的答案也可能是javascript
。您可以直接对隐藏元素执行javascript
并设置属性。
WebDriver driver;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('auth_login_password').setAttribute('value', val );");
driver.ExecuteScript(string.Format("document.getElementById('cred-password-inputtext').value='{0}';",password));
这为我解决了问题