javascript 如何使用javascript获取webdriver中的隐藏值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19385721/
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
how to get hidden values in webdriver using javascript
提问by Sunil Kumar
There's a hidden input field in which I'm trying to insert a specific date value. The field originally produces a value, from which a user can select an appropriate value. The page's source code looks like this:
有一个隐藏的输入字段,我试图在其中插入特定的日期值。该字段最初产生一个值,用户可以从中选择合适的值。该页面的源代码如下所示:
<div id="change_img">
<img width="80" height="30" border="1" src="http://jntuh.ac.in/results/images/CaptchaSecurityImages.php?width=100&height=50&characters=5&code=ryyrh">
<br>
<input id="code" type="hidden" value="ryyrh" name="code">
</div>
回答by JacekM
Use WebElement's getAttribute
method. In your case it will be:
使用 WebElement 的getAttribute
方法。在您的情况下,它将是:
WebElement hiddenInput = driver.findElement(By.id("code"));
String value = hiddenInput.getAttribute("value");
If for any reason you need to do it with javascript (your question specifically asked for js) then this code should work:
如果出于任何原因您需要使用 javascript(您的问题专门针对 js)执行此操作,那么此代码应该可以工作:
String script = "return document.getElementById('code').getAttribute('value');";
String value = ((JavascriptExecutor) driver).executeScript(script).toString();
回答by jibbs
I tested this solution in C# and it works. I am then able to parse the returned string to find and verify what I need.
我在 C# 中测试了这个解决方案并且它有效。然后我就可以解析返回的字符串以查找并验证我需要的内容。
http://yizeng.me/2014/04/08/get-text-from-hidden-elements-using-selenium-webdriver/
http://yizeng.me/2014/04/08/get-text-from-hidden-elements-using-selenium-webdriver/
So in the example in the question you would get the innerHTML of the visible parent "change_img" element which will include the hidden element.
因此,在问题中的示例中,您将获得包含隐藏元素的可见父元素“change_img”的 innerHTML。