消息:尝试通过 Selenium 单击下拉菜单中的选项时,元素 <option> 无法滚动到视图中
- 2025-02-25 09:07:00
- admin 原创
- 22
问题描述:
我正在尝试选择一个下拉菜单并选择一个选项。我正在使用最新版本的 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>
打印语句用于测试/代码跟踪目的。
解决方案 1:
此错误信息...
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view.
...意味着<option>
您的程序尝试交互的项目无法滚动到视图中。
所需元素的HTML可以让我们知道错误背后的原因。但是,所需元素似乎不在Viewportclickable
内。要解决此问题,您必须使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
解决方案 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
解决方案 3:
我遇到了同样的问题。我无法使用 selectByValue、selectByIndex 和 selectByVisible 文本选择下拉值。我也尝试使用 JavaScript 执行器执行操作类和 scrollBy。但都不起作用。经过一天的反复尝试,我想出了以下解决方法,
public static void selectDropDownByVisibleText(WebElement element, String text){
waitForPageLoad();
String firstCharacterDropDownVal = ""+text.charAt(0);
HashSet<String> uniqueDropDownVals = new HashSet<>();
try {
Select select = new Select(element);
select.selectByVisibleText(text);
}catch (ElementNotInteractableException e){
Select select = new Select(element);
while(!select.getFirstSelectedOption().getText().equalsIgnoreCase(text) &&
uniqueDropDownVals.add(select.getFirstSelectedOption().getText())) {
element.sendKeys(firstCharacterDropDownVal);
}
}
log.info("Selected dropdown by visible text "+text);
}
public static void waitForPageLoad() {
log.debug("Waiting for page to get loaded..");
new WebDriverWait(Driver.getDriver(), Duration.ofSeconds(FIND_ELEMENT_WAIT_TIME)).
ignoring(NoSuchElementException.class).ignoring(StaleElementReferenceException.class).
until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
log.debug("Page loaded successfully");
}
我将解释其工作原理。首先,它将尝试通过可见文本进行选择。如果出现异常,它将进入 catch 块内部。在那里,我们在下拉元素上发送所需文本的第一个字符。因此下拉元素会发生变化。假设下拉列表值为 Apple、Aeroplane、Adam,我想选择 Adam。我第一次在元素上发送“A”时,下拉列表更改为 Apple。然后我将它添加到 HashSet 并检查所选元素是否是所需元素。如果匹配,则我们跳出 while 循环。如果相同的元素被添加两次,则 HashSet add 方法将返回 false。这将使我们跳出 while 循环并防止我们进入无限循环。因此,每次我输入“A”时,下拉值都会更改为下一个以“A”开头的值。这就是方法。