消息:元素<选项>尝试通过 Selenium 单击下拉菜单中的选项时无法滚动到视图中 [英] Message: Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium
问题描述
我正在尝试选择一个下拉菜单并选择一个选项.我正在使用最新版本的 Selenium、最新版本的 Firefox、最新版本的 geckodriver 和最新版本的 Python.
I am trying to select a drop down menu and choose an option. I am using the latest version of Selenium, the latest version of Firefox, the latest version of geckodriver, and the latest version of Python.
这是我的问题:当我尝试选择一个选项时,出现以下错误:
Here is my issue: When I try to choose an option, it gives me the following error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
我尝试了各种方法来解决这个问题,但似乎都没有奏效.以下是我尝试过的一些方法.
I have tried various ways to circumnavigate this issue, but none seem to work. Here are some of the approaches I tried.
mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDownMenu = Select(mySelectElement)
dropDownMenu.select_by_visible_text('Professional')
mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDown = Select(mySelectElement)
for option in dropDown.options:
message = option.get_attribute('innerText')
print(message)
if message == 'Professional':
print("Exists")
dropDown.select_by_visible_text(message)
break
element = browser.find_element_by_id('providerTypeDropDown')
browser.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, "Professional")
HTML 代码遵循通常的选择标签和选项标签.任何帮助表示赞赏.HTML 代码包含在下面.
The HTML code follows the usual select tags and option tags. Any help is appreciated. The HTML code is included below.
<select data-av-chosen="providerTypes" id="providerTypeDropDown" data-placeholder="Please Select a Provider Type" name="providerTypeDropDown"
class="chzn-select input-full ng-pristine chzn-done ng-invalid ng-invalid-provider-type" data-ng-options="providerType.value for providerType in request.models.providerTypes"
data-ng-model="request.models.providerType" data-av-validator-field="providerType" data-disable-search-threshold="5" style="display; none;">
<option value="" class="">Please Select a Provider Type</option>
<option value="0">Professional</option>
<option value="1">Institutional</option>
</select>
打印语句用于测试/代码跟踪.
The print statements are there for testing/code tracing purposed.
推荐答案
此错误信息...
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
...表示您的程序试图与之交互的 项无法滚动到视图中.
...implies that the <option>
item which your program was trying to interact with could not be scrolled into view.
所需元素的 HTML 会给我们一些错误背后的想法.然而,似乎所需的元素不是 clickable
/视口.要解决此问题,您必须将 WebDriverWait 引入 元素以使其可点击,您可以使用以下解决方案:
The HTML of the desired element would have given us some idea behind the error. However it seems the desired element was not clickable
/ within the Viewport. To address the issue you have to induce WebDriverWait for the element to be clickable and you can use the following solution:
mySelectElement = browser.find_element_by_id('providerTypeDropDown')
dropDownMenu = Select(mySelectElement)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='providerTypeDropDown']//options[contains(.,'Professional')]")))
dropDownMenu.select_by_visible_text('Professional')
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
这篇关于消息:元素<选项>尝试通过 Selenium 单击下拉菜单中的选项时无法滚动到视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!