python input输入值掩盖_Selenium(Python):如何在隐藏输入上插入值?

这篇博客介绍了如何在使用Python的Selenium WebDriver时,通过执行JavaScript来向网页中的隐藏输入字段插入特定日期值。由于常规用户无法直接操作隐藏输入,因此需要利用JavaScript作为工作-around。示例代码展示了如何找到隐藏输入元素,并用JavaScript设置其值。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

I'm using Selenium's WebDriver and coding in Python.

There's a hidden input field in which I'm trying to insert a specific date value. The field originally produces a calendar, from which a user can select an appropriate date, but that seems like a more complicated endeavour to emulate than inserting the appropriate date value directly.

The page's source code looks like this:

where value="2013-11-26" is the field I'm trying to inject a value (it's originally empty, ie: value="".

I understand that WebDriver is not able to insert a value into a hidden input, because regular users would not be able to do that in a browser, but a workaround is to use Javascript. Unfortunately that's a language I'm not familiar with. Would anyone know what would work?

解决方案

You can use WebDriver.execute_script. For example:

from selenium import webdriver

driver = webdriver.Firefox()

driver.get('http://jsfiddle.net/falsetru/mLGnB/show/')

elem = driver.find_element_by_css_selector('div.dijitReset>input[type=hidden]')

driver.execute_script('''

var elem = arguments[0];

var value = arguments[1];

elem.value = value;

''', elem, '2013-11-26')

UPDATE

from selenium import webdriver

driver = webdriver.Firefox()

driver.get('http://matrix.itasoftware.com/')

elem = driver.find_element_by_xpath(

'.//input[@id="ita_form_date_DateTextBox_0"]'

'/following-sibling::input[@type="hidden"]')

value = driver.execute_script('return arguments[0].value;', elem)

print("Before update, hidden input value = {}".format(value))

driver.execute_script('''

var elem = arguments[0];

var value = arguments[1];

elem.value = value;

''', elem, '2013-11-26')

value = driver.execute_script('return arguments[0].value;', elem)

print("After update, hidden input value = {}".format(value))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值