简体   繁体   中英

Java selenium can't sendkeys and click element

I want to try to login to this website

This is my code :

driver.findElement(By.id("userid_sebenarnya")).sendKeys("myUserName");

This is property for input text with 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>

But I got error message like bellow, whereas it should be found if I look visually.

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"userid_sebenarnya"}

Is this related to the hidden text input, what can I do?

  1. Your webpage login request is under a frame so first we need to switch to the frame and then find element.

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

JAVA Code:

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. Login directly with the url below. It does not contain a frame:

    This link

Try to use this xpath instead of 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();

I checked this on the given website and it worked for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM