Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

java - How to enter password using Selenium webdriver if the password style is display:none

I have a page with login and password (betmarathon[d.o.t]com). I want to login to the site using selenium webdriver. Selenium enters the login correctly, but i have problems with entering password. I get "Element not visible" exception.

My code for selenium looks like this:

driver.findElement(By.id("auth_login")).sendKeys("MY-USERNAME");
driver.findElement(By.id("auth_login_password")).sendKeys("MY-PASSWORD");

Html code of the page looks like this:

<div class="user">
<input id="auth_login" class="empty" type="text" maxlength="40" rel="Login:" name="login" tabindex="1">
</div>
<div class="pass">
<input id="auth_login_password" type="password" regex="^.{6,}$" maxlength="100" rel="Password:" name="login_password" tabindex="2" style="display: none;">
<input class="undefined empty" type="text" value="Password:" tabindex="2" style="display: inline;">
</div>

You can see that there are 2 inputs for password, the first one is not visible and the second is visible. I should enter the password in the first input. After I click on the box manually, the html code changes and the first input for password becomes visible (display:inline) and the second changes to display:none. But how can I do it with selenium webdriver?

Thanks a lot in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Click the second password input and then send keys to the first one:

driver.findElement(By.xpath("//div[@class='pass']/input[last()]")).click();
driver.findElement(By.id("auth_login_password")).sendKeys("MY-PASSWORD");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...