简体   繁体   English

如何使用 Java 在 Selenium WebDriver 的隐藏字段中输入一些文本

[英]How to type some text in hidden field in Selenium WebDriver using Java

I am using WebDriver with Java for test automation.我正在使用带有 Java 的 WebDriver 进行测试自动化。 I have the following HTML code for input field which is hidden:我有以下隐藏的输入字段的 HTML 代码:

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

How to type something in hidden field in Selenium2 (WebDriver)?如何在 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但显示以下错误:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间或超时交互:30.04 秒

Can anybody please help me to write/type some text in hidden field?有人可以帮我在隐藏字段中写/输入一些文本吗?

First of all you have to change the value of type attribute as text from hidden.首先,您必须将 type 属性的值更改为隐藏的文本。 The following code using javascript would work for that:使用 javascript 的以下代码将适用于此:

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

Now, you are able to type on that text by using WebDriver.现在,您可以使用 WebDriver 键入该文本。 So, the overall code for typing in a hidden field with WebDriver using Java and Javascript as follows:因此,使用 Java 和 Javascript 使用 WebDriver 在隐藏字段中键入的整体代码如下:

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");
WebDriver driver=new FirefoxDriver();
driver.get("http://localhost/login.do");
driver.manage().window().maximize();
RemoteWebDriver r=(RemoteWebDriver) driver;
String s1="document.getElementById('username').value='admin'";
r.executeScript(s1);

you need to initialize a JavascriptExecutor, which will perform a javaScript command:您需要初始化一个 JavascriptExecutor,它将执行一个 javaScript 命令:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('ElementId').setAttribute('type','text');");
driver.findElement(By.id("ElementId")).click();
driver.findElement(By.id("ElementId")).clear();
driver.findElement(By.id("ElementId")).sendKeys("theTextYouWant");

and if you want to hide it :如果你想隐藏它:

js.executeScript("document.getElementById('ElementId').setAttribute('type','hidden');");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中使用Selenium Webdriver获取更新的隐藏字段值 - how to get updated hidden field value using Selenium webdriver in java 如何使用Java在Selenium2(Webdriver)中键入Gmail正文文本 - How to type Gmail Body text in Selenium2 (Webdriver) using Java 如何使用 Java 在 Selenium WebDriver 中选择隐藏的下拉值 - How to select a hidden dropdown value in Selenium WebDriver using Java 如何使用Java Selenium Webdriver访问隐藏的下拉菜单 - How to access hidden dropdown menu using Java Selenium webdriver 如何删除文本字段中的默认值,并使用Selenium WebDriver键入新值 - How to delete the default value in a text field and type a new value using Selenium WebDriver 如何使用Selenium WebDriver(Selenium 2)和Java键入文本框? - How to type in textbox using Selenium WebDriver (Selenium 2) with Java? Java/Selenium WebDriver:如何获取用户输入并将其插入到文本字段中? - Java/Selenium WebDriver: How to get user input and insert it in to a text field? 使用Java + Selenium WebDriver获取一些文本 - Get some text with java + selenium WebDriver 在 Eclipse 中使用 Selenium WebDriver 和 java 代码在搜索字段中输入文本后如何按“Enter” - How to press 'Enter' once text is entered in the search field using Selenium WebDriver and java code in eclipse 如何使用Java和Selenium2 / Webdriver将明天的日期输入文本字段 - How can I enter tomorrow's date into a text field using Java and Selenium2/Webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM