admin管理员组文章数量:1327843
I am trying to execute a javascript
using python selenium. I am basically trying to set the value using execute.script
but somehow is it not doing anything. I want to edit the street address as you see below
execute_script(driver, """var element = document.getElementsByClassName('input[ng-model="formData.address.address1"]'); element.value = '328 91st Street'; """)
Could anyone tell me what's the issue here? I am not getting an error also
I am trying to execute a javascript
using python selenium. I am basically trying to set the value using execute.script
but somehow is it not doing anything. I want to edit the street address as you see below
execute_script(driver, """var element = document.getElementsByClassName('input[ng-model="formData.address.address1"]'); element.value = '328 91st Street'; """)
Could anyone tell me what's the issue here? I am not getting an error also
Share Improve this question asked Feb 19, 2016 at 0:01 pythonpython 4,52114 gold badges60 silver badges109 bronze badges 4-
In case you don't know....
getElementsByClassName
will return you an array.... so you must do element[0] (if you want the first element) – Alvaro Silvino Commented Feb 19, 2016 at 0:14 -
is there any way I can print out the element array? I am getting an error
WebDriverException: Message: unknown error: Cannot set property 'value' of undefined
– python Commented Feb 19, 2016 at 0:19 -
You can debug like
console.log(element)
and you will get the array... – Alvaro Silvino Commented Feb 19, 2016 at 0:22 -
I just updated the answer... you should use
querySelector
instead ofgetElementsByClassName
– Alvaro Silvino Commented Feb 19, 2016 at 0:25
2 Answers
Reset to default 3There is a more robust way of doing it - locating the element with selenium using a CSS selector and passing the WebElement
as well as a value into the script as an argument:
elm = driver.find_element_by_css_selector('input[ng-model="formData.address.address1"]')
value = '328 91st Street'
driver.execute_script("arguments[0].value = 'arguments[1]';", elm, value)
Note that in your code, you have 2 major problems:
- you are passing a CSS selector into the
getElementsByClassName()
call - instead, it expects you to pass a class name as a string getElementsByClassName()
returns an array of elements and not a single element
This code is almost good to go...
execute_script(driver, """var element = document.getElementsByClassName('input[ng-model="formData.address.address1"]'); element.value = '328 91st Street'; """)
Just remember that getElementsByClassName
will return an array...
And I guess you should use querySelector
or querySelectorAll
function...
// will select just one element
var element = document.querySelector('input[ng-model="formData.address.address1"]');
// will select all elements
var element = document.querySelectorAll('input[ng-model="formData.address.address1"]');
getElementsByClassName
you should inform a class... (I think it's hard to have a class like: ng-model="formData.address.address1"
)
Using querySelector
var element = document.querySelector('input[ng-model="formData.address.address1"]');
element.value = '328 91st Street';//Work!!!
In case you want to iterate in these NodeLists with querySelectorAll
Basically,
var element = document.querySelectorAll('input[ng-model="formData.address.address1"]');
element.value = '328 91st Street';//WON'T WORK
Do instead:
var element = document.querySelectorAll('input[ng-model="formData.address.address1"]');
element[0].value = '328 91st Street'; // change the value for the first element
for(int i = 0 ;i<element.length;i++){ //change all elements element[i].value = '328 91st Street'; }
本文标签: How to use javascript with selenium pythonStack Overflow
版权声明:本文标题:How to use javascript with selenium python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742215073a2434421.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论