How to use java to type some text in the hidden field of selenium webdriver

I use webdriver and Java for test automation I have the following HTML code for hidden input fields:

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

How do I enter content in the hidden field of selenium 2 (webdriver)? I wrote the following code:

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

However, the following error is displayed: org openqa. selenium. Elementnotvisibleexception: the element is not currently visible, so it may not be associated with the command duration or timeout: 30.04 seconds

Can someone help me write / hide some text in the field?

Solution

First, you must change the value of the type attribute from the hidden text The following code using JavaScript will apply to:

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

You can now type this text using webdriver Therefore, the overall code for typing with Java and JavaScript and webdriver is 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");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>