Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - How to type some text in hidden field in Selenium WebDriver using Java

I am using WebDriver with Java for test automation. I have the following HTML code for input field which is hidden:

<input type="hidden" value="" name="body" id=":6b">

How to type something in hidden field in Selenium2 (WebDriver)? I have written code as:

driver.findElement(By.name("body")).sendKeys("test body");

But it was shown the following error: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.04 seconds

Can anybody please help me to write/type some text in hidden field?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

First of all you have to change the value of type attribute as text from hidden. The following code using javascript would work for that:

jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");

Now, you are able to type on that text by using WebDriver. So, the overall code for typing in a hidden field with WebDriver using Java and Javascript as follows:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementsByName('body')[0].setAttribute('type', 'text');");
driver.findElement(By.xpath("//input[@name='body']")).clear();
driver.findElement(By.xpath("//input[@name='body']")).sendKeys("Ripon: body text");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...