我正在尝试选择一个下拉菜单并选择一个选项。我正在使用最新版本的 Selenium、最新版本的 Firefox、最新版本的 geckodriver 和最新版本的 Python。
这是我的问题:当我尝试选择一个选项时,它给了我以下错误:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
我尝试了各种方法来解决这个问题,但似乎都没有奏效。以下是我尝试过的一些方法。
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 代码包含在下面。
<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>
打印语句用于测试/代码跟踪。
最佳答案
这个错误信息...
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
...暗示
<option>
您的程序试图与之交互的项目无法滚动到 View 中。所需元素的 HTML 会给我们一些错误背后的想法。然而,似乎所需的元素不是
clickable
/内Viewport .要解决该问题,您必须引入 WebDriverWait 以使元素可点击,您可以使用以下解决方案: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')
备注 :您必须添加以下导入:
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
关于python - 消息 : Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250668/