信息: 在尝试通过Selenium点击下拉菜单中的一个选项时,<option>元素无法滚动到视图中。

2024-04-25 00:05:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图选择一个下拉菜单并选择一个选项。我使用的是最新版本的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代码遵循通常的select标记和option标记。如有任何帮助,我们将不胜感激。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> 

打印语句用于测试/代码跟踪。


Tags: 版本browseridmessagedatabyelementng
2条回答

尝试添加等待:

mySelectElement = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "providerTypeDropDown")))
mySelectElement.click()

它将至少等待10秒,直到元素可以单击,然后单击。

另外,我在你的代码中没有看到,你点击了下拉按钮,它打开了下拉菜单。找到此按钮,也添加一个等待并在选择选项之前单击它。希望有帮助。

注意:对于此代码,必须添加一些导入:

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

此错误消息。。。

selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.

…表示程序试图与之交互的<option>项无法滚动到视图中。

所需元素的HTML会给我们一些错误背后的想法。然而,似乎所需的元素不是Viewport中的clickable/。要解决此问题,您必须诱导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

相关问题 更多 >