admin管理员组

文章数量:1422437

I am trying to sendkeys to an input,but I don't know why it warns me like this:

org.openqa.selenium.InvalidElementStateException: Element must not be hidden, disabled or read-only (WARNING: The server did not provide any stacktrace information)

The HTML page source:

<span id="mini-7" class="mini-textbox mini-textbox-empty" style="border-width: 0pt; width: 342px;">
<input class="mini-textbox-input" type="text" autoplete="off" style="width: 338px;">
<input type="hidden">
</span>

My code:

driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).clear();
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).sendKeys("yy");

Then I change my code like this:

JavascriptExecutor jse = (JavascriptExecutor)driver;
((JavascriptExecutor) jse).executeScript("arguments[0].type ='text';",driver.findElement(By.xpath("//span[@id='mini-7']/input[2]")));   

But this time it throws out js error. Why?

I use sendkeys to the first input,this input is not hidden

I am trying to sendkeys to an input,but I don't know why it warns me like this:

org.openqa.selenium.InvalidElementStateException: Element must not be hidden, disabled or read-only (WARNING: The server did not provide any stacktrace information)

The HTML page source:

<span id="mini-7" class="mini-textbox mini-textbox-empty" style="border-width: 0pt; width: 342px;">
<input class="mini-textbox-input" type="text" autoplete="off" style="width: 338px;">
<input type="hidden">
</span>

My code:

driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).clear();
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).sendKeys("yy");

Then I change my code like this:

JavascriptExecutor jse = (JavascriptExecutor)driver;
((JavascriptExecutor) jse).executeScript("arguments[0].type ='text';",driver.findElement(By.xpath("//span[@id='mini-7']/input[2]")));   

But this time it throws out js error. Why?

I use sendkeys to the first input,this input is not hidden

Share Improve this question edited Apr 2, 2013 at 4:43 user2027659 asked Dec 25, 2012 at 5:36 怡 许怡 许 211 gold badge1 silver badge2 bronze badges 3
  • did you try with capitalized "HIDDEN" ? IE has some issues – cppanda Commented Dec 25, 2012 at 5:47
  • I fixed it ,just like this:JavascriptExecutor jse = (JavascriptExecutor)driver; ((JavascriptExecutor) jse).executeScript("arguments[0].style.visibility = 'visible';",driver.findElement(By.xpath("//span[@id='mini-7']/input"))); but i still donot know why – 怡 许 Commented Dec 25, 2012 at 8:26
  • What language did you use? Java? C#? or what? – Ripon Al Wasim Commented Jan 22, 2013 at 11:32
Add a ment  | 

3 Answers 3

Reset to default 1

That input could still have css property visibility: hidden or display: none. That's what exception is telling you. Check all the properties with browser's dev tools.

First of all it needs to change the value of type attribute as text from hidden. Now, you are able to type on that text by using WebDriver. So, the overall code for typing with WebDriver using Java and Javascript as follows:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('mini-7').setAttribute('type', 'text');");
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).clear();
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).sendKeys("yy");

For 1st input field which is not hidden, use absolute path for cssSelector.

What you can do in this case is making the element visible by modifying the css tag for

visibility: hidden 

or

display: none

using javascript just before sending keys to it.

本文标签: javascriptHow to type text on hidden text box in WebDriver with JavaStack Overflow