gpt4 book ai didi

java - 无法在 Java 中使用 selenium webdriver 登录

转载 作者:行者123 更新时间:2023-12-02 11:28:31 25 4
gpt4 key购买 nike

这真的很奇怪,但我突然无法使用 Selenium 登录。一周前我还可以使用我的代码登录,然后就不能再登录了。以下是该网站的 html 代码:

<div class="form-group">
<label for="loginUsername" style="width: 100%; font-weight: normal" aria-label="Username">
<input id="loginUsername" class="form-control" name="username" type="text" placeholder="Username" autocapitalize="off">
</label>
</div>

<div class="form-group">
<label for="loginPassword" style="width: 100%; font-weight: normal" aria-label="Password">
<input id="loginPassword" class="form-control" name="password" type="password" placeholder="Password" autocapitalize="off">
</label>
</div>

这是我在事件发生前的代码:

driver.findElement(By.xpath("//*[@id=\"loginUsername\"]")).sendKeys("username");
driver.findElement(By.xpath("//*[@id=\"loginPassword\"]")).sendKeys("password");

之前还好好的。我什至尝试改变它,它看起来像这样:

WebElement username = driver.findElement(By.xpath("//*[@id=\"loginUsername\"]")); username.click(); username.clear(); ;username.sendKeys("username");
WebElement password = driver.findElement(By.xpath("//*[@id=\"loginPassword\"]")); password.click(); password.clear(); ;password.sendKeys("password");

老实说,我不知道我的代码有什么问题,而且我也没有收到任何错误。我也尝试过使用“By.name()”或“By.id()”,但它仍然不起作用,所以我真的很困惑。请帮助我,谢谢!

已编辑这是代码的完整版本

<div class="col-sm-4 col-sm-offset-4">
<form action="/login" method="post">
<div class="form-group title">
<img src="/img/RA_LOGO_BLUE.png" alt="Risk Assessment System Logo">
</div>
<div class="form-group">
<label for="loginUsername" style="width: 100%; font-weight: normal" aria-label="Username">
<input id="loginUsername" class="form-control" name="username" type="text" placeholder="Username" autocapitalize="off">
</label>
</div>
<div class="form-group">
<label for="loginPassword" style="width: 100%; font-weight: normal" aria-label="Password">
<input id="loginPassword" class="form-control" name="password" type="password" placeholder="Password" autocapitalize="off">
</label>
</div>
<input id="loginBtn" class="btn" type="submit" value="Login">
</form>
<br>
<div class="text-left">
<span class="fakeLink" data-toggle="modal" data-target="#forgotPasswordModal">Forgot Password?</span><br>
<span class="fakeLink" data-toggle="modal" data-target="#forgotUsernameModal">Forgot Username?</span>
</div>
</div>

最佳答案

要将文本发送到用户名密码字段,您可以使用以下代码块:

driver.findElement(By.xpath("//input[@id='loginUsername']")).sendKeys("username");
driver.findElement(By.xpath("//input[@id='loginPassword']")).sendKeys("password");

关于java - 无法在 Java 中使用 selenium webdriver 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49456575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com