如何使用 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:元素当前不可见,因此可能无法与之交互命令持续时间或超时: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 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");
这篇关于如何使用 Java 在 Selenium WebDriver 的隐藏字段中输入一些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!