在Selenium Python中为输入元素设置值 [英] Setting value for input element in selenium Python

查看:1130
本文介绍了在Selenium Python中为输入元素设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改输入标签元素的值.

I am trying to change the value of an input tag element.

这是标签:<input type="hidden" id="hiRequestAccessType" data-bind="value: requestAccessTypeStr" value="2">

我想将值更改为"2,1".

I want to change value to "2,1".

基于上的讨论设置sendKeys()-硒webdriver nodejs 的示例,我尝试使用execute_script,但值保持不变.

Based on discussion at Set value of input instead of sendKeys() - selenium webdriver nodejs, I tried using execute_script, but value remains unchanged.

我尝试过:

passwordcheck_input_element = driver.find_element_by_xpath('//*[@id="hiRequestAccessType"]') . ###THIS DOESNT THROW ERRORS
new_value = "2,1"

driver.execute_script("arguments[0].value = arguments[1].toString()", passwordcheck_input_element, new_value)
# driver.execute_script("arguments[0].value = '" + new_value + "'", passwordcheck_input_element) . ###TRIED THIS IN LIEU OF ABOVE

对于任何一种选择,代码都可以运行,但是通过目测检查,值保持不变.我还尝试了使用'setAttribute'而不是直接使用相同的结果(不变)的上述两种选择.

For either alternative, code runs but value remains unchanged from visual inspection. I also tried the above two alternatives using 'setAttribute' instead of directly, same (no change) result.

请注意,该网页是一种形式,其中单击复选框可以根据需要将值更改为"2,1". (但是,如果我尝试找到复选框元素,则会收到消息,提示它不可单击,因此是此路由).

Note that the webpage is a form where clicking on a check box changes value to "2,1" as desired. (But if I try finding the check box element, I get the message it is not clickable, hence this route).

现在,奇怪的是,我知道它在幕后做一些事情,因为我在execute_script调用之前和之后尝试查询value属性,并为后者正确打印了新值.但是,正如我所说,UI并未显示此更改;此外,当我继续前进并进一步点击提交按钮时,使用的是旧值,因为如果使用了新值,我没有得到应加载的页面.

Now, what's weird is I know it's doing something right behind the scenes because I tried querying value attribute before and after my execute_script call and it prints out the new value correctly for latter. However, as I said, the UI doesnt show this change; further, when I move on and hit the submit buttom further down, it's the old value that gets used because I am not getting the page that should load if the new value were used.

推荐答案

能否请您尝试下面的代码?

Could you please try the code below?

passwordcheck_input_element = driver.find_element_by_id("hiRequestAccessType")
driver.execute_script("arguments[0].value = '2,1';", passwordcheck_input_element) 

如果无法点击,您还可以通过JavaScript执行来控制该复选框对象.

You can also control the checkbox object via javascript execution if it is not clickable.

driver.execute_script("document.getElementById('hiRequestAccessType').checked = true;")

这篇关于在Selenium Python中为输入元素设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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