javascript 如何使用 Java 在 WebDriver 中的隐藏文本框中键入文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14027998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:32:27  来源:igfitidea点击:

How to type text on hidden text box in WebDriver with Java

javajavascriptinputselenium-webdriverhidden-field

提问by u1927672

I am trying to sendkeys to an input,but I don't know why it warns me like this:

我正在尝试将密钥发送到输入,但我不知道为什么它会这样警告我:

org.openqa.selenium.InvalidElementStateException: Element must not be hidden, disabled or read-only (WARNING: The server did not provide any stacktrace information)

org.openqa.selenium.InvalidElementStateException:元素不得隐藏、禁用或只读(警告:服务器未提供任何堆栈跟踪信息)

The HTML page source:

HTML 页面源代码:

<span id="mini-7" class="mini-textbox mini-textbox-empty" style="border-width: 0pt; width: 342px;">
<input class="mini-textbox-input" type="text" autocomplete="off" style="width: 338px;">
<input type="hidden">
</span>

My code:

我的代码:

driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).clear();
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).sendKeys("yy");

Then I change my code like this:

然后我像这样更改我的代码:

JavascriptExecutor jse = (JavascriptExecutor)driver;
((JavascriptExecutor) jse).executeScript("arguments[0].type ='text';",driver.findElement(By.xpath("//span[@id='mini-7']/input[2]")));   

But this time it throws out js error. Why?

但是这次它抛出了js错误。为什么?

I use sendkeys to the first input,this input is not hidden

我使用sendkeys到第一个输入,这个输入没有隐藏

回答by Alex Okrushko

That input could still have css property visibility: hiddenor display: none. That's what exception is telling you. Check all the properties with browser's dev tools.

该输入仍然可以具有 css 属性visibility: hiddendisplay: none. 这就是例外告诉你的。使用浏览器的开发工具检查所有属性。

回答by Ripon Al Wasim

First of all it needs to change the value of type attribute as text from hidden. 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:

首先,它需要将 type 属性的值更改为隐藏的文本。现在,您可以使用 WebDriver 键入该文本。因此,使用 Java 和 Javascript 使用 WebDriver 打字的整体代码如下:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('mini-7').setAttribute('type', 'text');");
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).clear();
driver.findElement(By.cssSelector("#mini-7 > input.mini-textbox-input")).sendKeys("yy");

For 1st input field which is not hidden, use absolute path for cssSelector.

对于未隐藏的第一个输入字段,请使用 cssSelector 的绝对路径。

回答by Santiago Hernandez

What you can do in this case is making the element visible by modifying the css tag for

在这种情况下你可以做的是通过修改 css 标签来使元素可见

visibility: hidden 

or

或者

display: none

using javascript just before sending keys to it.

在向其发送密钥之前使用 javascript。