简体   繁体   English

如何在python和selenium中使用execute_script从下拉列表中选择一个值

[英]How to use execute_script in python and selenium to select a value from drop down

I have a form whose elements return elementnotinteractable exception when I use selenium operations like click() and sen_keys. 我有一种形式,当我使用诸如click()和sen_keys之类的硒操作时,其元素返回elementnotinteractable异常。

I used execute_script to interact with elements and it works fine. 我使用execute_script与元素进行交互,并且效果很好。 However, I am unable to perform a select from dropdown operation. 但是,我无法执行下拉菜单中的选择。 I like an execute_script equivalent of following statement - 我喜欢相当于以下语句的execute_script-

path = ".//div[@id='web_service_dlg']//select[@name='tenancy']/option[@value='Customer Tenancy']"
self.browser.find_element_by_xpath(path).click()

Above statement fails with the exception elementnotinteractable I am trying to solve the problem by doing the following - 上面的语句失败,异常元素不可互操作我试图通过执行以下操作来解决问题 -

element = self.browser.find_element_by_xpath(".//div[@id='web_service_dlg']//select[@name='tenancy']/option[@value='Customer Tenancy']")
self.browser.execute_script("return arguments[0].scrollIntoView(true);", element)
self.browser.execute_script("return arguments[0].removeAttribute('onkeypress');", element)
self.browser.execute_script("return arguments[0].click();", element)

Above statement does not give me any error, but it is not selecting the element I want. 上面的语句没有给我任何错误,但是它没有选择我想要的元素。 What am I doing wrong? 我究竟做错了什么?

用户selected=true

self.browser.execute_script("return arguments[0].selected=true;", element)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Selenium Python execute_script 中获取返回元素的值? - How to get value of returned element in Selenium Python execute_script? Python Selenium如何在带参数的JavaScript元素上执行execute_script - Python selenium how to execute execute_script on a JavaScript element with arguments 如何将jQuery与硒execute_script方法一起使用? - How can I use jQuery with selenium execute_script method? Python和Selenium以“execute_script”来解决“ElementNotVisibleException” - Python and Selenium To “execute_script” to solve “ElementNotVisibleException” 如何使用 Python+Selenium 从 execute_script 获取 JS 控制台响应代码? - How do I get JS Console response code from execute_script with Python+Selenium? 方法execute_script无需等待脚本结束即可在python中使用硒返回值 - method execute_script don't wait end of script to return value with selenium in python 带换行符的 selenium execute_script - selenium execute_script with newlines 从 Selenium execute_script 获取无值/错误,从 ASX.au web 抓取 - Getting None Value/Error from Selenium execute_script, scraping from ASX.au web 更改 src 属性 execute_script selenium python - Change src attribute execute_script selenium python Python Selenium:execute_script 是否可以等待 javascript 内容加载? - Python Selenium : is execute_script able to wait for javascript content to load?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM