我想尝试登录 this website
这是我的代码:
driver.findElement(By.id("userid_sebenarnya")).sendKeys("myUserName");
这是输入文本的属性 id='userid_sebenarnya'
<div class="form-group" id="form-group-height">
<label for="userid" class="text-field-label-horizontal-empty">User ID</label>
<input id="userid" class="fake_field_userid" style="-webkit-box-shadow: inset 0 0 0 2em transparent !important" value="autofill field" type="text">
<div class="outer-border-login">
<input class="form-control-login-transparent" id="userid_sebenarnya" placeholder="Masukkan user ID" onblur="removeErrMsg('#userid');" data-rule-required="true" data-msg-required="Field ini dibutuhkan" autocomplete="off" readonly="" onfocus="this.removeAttribute('readonly'); this.focus();" value="" autocorrect="off" autocapitalize="off" spellcheck="false" maxlength="2147483647" style="-webkit-box-shadow: inset 0 0 0 2em transparent !important" type="text">
</div>
</div>
但是我收到了如下所示的错误消息,而如果我目视查看应该可以找到它。
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"userid_sebenarnya"}
这是否与隐藏文本输入有关,我该怎么办?
最佳答案
您的网页登录请求位于框架下,因此我们首先需要切换到框架,然后找到元素。
< frameset > < frame src='/retail3/loginfo/loginRequest' name="mainFrame" scrolling="auto" noresize > </frameset>
JAVA代码:
driver.get("the_site");
driver.switchTo().frame("mainFrame");
driver.findElement(By.id("userid_sebenarnya")).sendKeys("myUserName");
driver.findElement(By.id("pwd_sebenarnya")).sendKeys("myUserName");
driver.findElement(By.id("btnSubmit")).click();
直接使用以下网址登录。它不包含框架:
关于Java selenium 无法发送按键和单击元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52293144/