简体   繁体   中英

Login via CSS Selector for Selenium Webdriver Java

I am new to Java and Selenium. I am trying to login to a site that has a unique id for username and password so I figured I would use css to locate the login fields. I am able to do this in Selenium IDE but not in Eclipse.

<input name="uid_578180ab6f188bd2803df958f4aafa6c399bc8fe" class="form-control 
col-sm-10" id="uid_578180ab6f188bd2803df958f4aafa6c399bc8fe" autofocus="" 
required="" type="text">

I am able to launch the website but the username and password fields remain blank.

driver.findElement(By.cssSelector("[id^=uid_]")).sendKeys("username");
driver.findelement(By.cssSelector("[id^=pwd_]")).sendKeys("password");

This is for the password field. Maybe my approach to this is incorrect.

<input type="password" required="" class="form-control" 
id="pwd_28e13d5a226a4a576f54de3e0501f2ce1b8204ee" 
name="pwd_28e13d5a226a4a576f54de3e0501f2ce1b8204ee" 
onkeypress="if(event.keyCode==13)
{document.getElementById('login_form').submit();}">

CSS - is styling tecnology, it used selectors (.classes and #id's) to locate elements. Java can use selectors to find elements. It's smart to use classes if you want to find many elements and ID's for unique (single) element.

driver.getElementById("uid_578180ab6f188bd2803df958f4aafa6c399bc8fe").sendKeys("username");
driver.getElementById("pwd_28e13d5a226a4a576f54de3e0501f2ce1b8204ee").sendKeys("password");

I have tried your solution. It should work if there is only one element that starts with " uid " or " pwd " on a web page.

You can try putting a little wait and check. If the path you have identified is unique, the text will be entered in both username and password fields

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