【问题标题】:Java selenium can't sendkeys and click elementJava selenium 不能发送键和点击元素
【发布时间】:2020-06-18 22:12:09
【问题描述】:

我想尝试登录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>

但我收到如下错误消息,但如果我用肉眼看应该会发现它。

线程“main”中的异常 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“id”,“selector”:“userid_sebenarnya”}

这是否与隐藏文本输入有关,我该怎么办?

【问题讨论】:

标签: java html selenium


【解决方案1】:
  1. 您的网页登录请求是在一个框架下,所以首先我们需要切换到框架,然后找到元素。

    &lt frameset &gt 
    &lt frame src='/retail3/loginfo/loginRequest' name="mainFrame" 
    scrolling="auto" noresize &gt
    &lt/frameset&gt
    

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();
  1. 使用下面的网址直接登录。它不包含框架:

    This link

【讨论】:

    【解决方案2】:

    尝试使用这个 xpath 而不是 id

    driver.findElement(By.xpath("//*[contains(@id,'userid_sebenarnya')]")).sendKeys("myUserName");
    driver.findElement(By.xpath("//*[contains(@id,'pwd_sebenarnya')]")).sendKeys("myPassword");
    driver.findElement(By.xpath("//*[contains(@id,'btnSubmit')]")).click();
    

    我在给定的网站上检查了这个,它对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2019-12-29
      • 2017-12-08
      相关资源
      最近更新 更多