如何使用Java在Selenium WebDriver中的隐藏字段中键入一些文本 [英] How to type some text in hidden field in Selenium WebDriver using Java
问题描述
我正在使用WebDriver和Java进行测试自动化。我有以下隐藏的输入字段的HTML代码:
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">
如何在Selenium2(WebDriver)的隐藏字段中输入内容?我编写的代码为:
How to type something in hidden field in Selenium2 (WebDriver)? I have written code as:
driver.findElement(By.name("body")).sendKeys("test body");
但是显示以下错误:
org.openqa.selenium.ElementNotVisibleException:Element目前不可见,因此可能无法与
进行交互命令持续时间或超时:30.04秒
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?
推荐答案
首先,您必须将type属性的值更改为隐藏的文本。使用javascript的以下代码适用于:
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');");
现在,您可以使用WebDriver键入该文本。因此,使用Java和Javascript键入WebDriver的整体代码如下:
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.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");
这篇关于如何使用Java在Selenium WebDriver中的隐藏字段中键入一些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!