How to set value of a hidden input with Selenium?

前端 未结 1 1559
栀梦
栀梦 2021-01-21 10:16

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.n         


        
相关标签:
1条回答
  • 2021-01-21 11:00

    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');")
    
    0 讨论(0)
提交回复
热议问题