如何使用Selenium设置隐藏的输入值? [英] How to set value of a hidden input with Selenium?

查看:219
本文介绍了如何使用Selenium设置隐藏的输入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过这个,但没有运气。 / p>

我试过这个例子,它说 undefined browserbot ,我也试过这个简单的:

  @ browser.navigate.tohttp://example.com
@ browser.execute_script($('#hiddenthing' ).val('foo bar'))

完全不起作用,如果我试图设置没有javascript的值,它说你不能与隐藏元素交互



有什么建议吗? Selenium WebDriver不能与隐藏元素交互,只能找到它们。如果您尝试在隐藏元素上进行任何基于用户的交互,您将看到上面显示的错误。

这是因为SWD是为了模拟用户可以直接做(除少数例外)。能够与隐藏元素进行交互不在SWD的范围之内。然而,SWD的确提供了将任何javascript注入浏览器的DOM的能力(这使得处理这些类型的要求更合理,如果只是稍微难一些)。

通过执行javascript(正如您从上面链接的线程中看到的)尝试这两种方法。请记住,它需要使用 return 命令:

  browser.execute_script(return document.getElementById('hiddinthing')。value ='foo';)



<如果你有jQuery的话,那么你可以使用jquery的方法, foo');)


I've already looked at this but had no luck.

I've tried that example and it says undefined browserbot, I also tried the simple:

@browser.navigate.to "http://example.com"
@browser.execute_script("$('#hiddenthing').val('foo bar')")

which doesn't work at all, If i tried to set the value without javascript, it says you can't interact with hidden elements.

Any suggestions?

解决方案

Selenium WebDriver cannot interact with hidden elements, it can only find them. If you attempt to do any user based interaction on a hidden element, you will get the error you saw above.

This is because SWD was created to emulate the things a user can directly do (with a few exceptions). Being able to interact with hidden elements falls outside the scope of SWD.

However, SWD does provide the ability to inject any javascript into the DOM of the browser (which makes handling these types of requirements more reasonable, if just a bit more difficult).

Try these two ways by executing javascript (as you saw from the above thread you linked to). Just remember that it requires the use of the return command:

@browser.execute_script("return document.getElementById('hiddinthing').value = 'foo';")

or if you do have jQuery

@browser.execute_script("return $('#hiddenthing').val('foo');")

这篇关于如何使用Selenium设置隐藏的输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆