简体   繁体   English

通过CSS Selector登录Selenium Webdriver Java

[英]Login via CSS Selector for Selenium Webdriver Java

I am new to Java and Selenium. 我是Java和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. 我正在尝试登录到具有用户名和密码唯一ID的站点,所以我想我会使用css来查找登录字段。 I am able to do this in Selenium IDE but not in Eclipse. 我可以在Selenium IDE中执行此操作,但不能在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. CSS - 是样式技术,它使用选择器(.classes和#id)来定位元素。 Java can use selectors to find elements. Java可以使用选择器来查找元素。 It's smart to use classes if you want to find many elements and ID's for unique (single) element. 如果要为唯一(单个)元素找到许多元素和ID,那么使用类是明智的。

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. 如果在网页上只有一个以“ uid ”或“ pwd ”开头的元素,它应该可以工作。

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 如果您标识的路径是唯一的,则将在用户名密码字段中输入文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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