尝试从Selenium Web Automation中的下拉菜单中选择一个选项-错误-"ElementNotInteractableException:无法滚动到视图中". [英] Trying to select an option from the dropdown in Selenium web automation -error- "ElementNotInteractableException: could not be scrolled into view"
问题描述
package com.web.automation;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class dropDown {
WebDriver driver;
@BeforeMethod
public void site() throws InterruptedException{
System.setProperty("webdriver.gecko.driver", "geckodriver");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.amazon.com/");
}
@AfterMethod
public void close(){
driver.close();
}
@Test
public void register() throws InterruptedException{
Select s = new Select(driver.findElement(By.xpath("//select[@id='searchDropdownBox']")));
s.selectByValue("search-alias=alexa-skills");
}
}
代码说明:
我正在尝试使 www.amazon.com 网页自动化.存在一个称为全部"的下拉列表.在首页本身中.如果单击所有下拉菜单,将有不同的选项可供选择.使用Selenium自动化,我试图单击下拉列表并选择选项之一.
I am trying to automate www.amazon.com web page. There is drop down list called "All" in the home page itself. if we click the All dropdown menu there will be different option to choose. Using Selenium automation I am trying to click the drop down and select one of the option.
Select s = new Select(driver.findElement(By.xpath("//select[@id='searchDropdownBox']")));
s.selectByValue("search-alias=alexa-skills");
错误:
FAILED: register
org.openqa.selenium.ElementNotInteractableException: Element <option> could not be scrolled into view
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
推荐答案
要从下拉列表中选择文本为 Books
的选项,您需要引入定位器策略:
To select the option with text as Books
from the dropdown you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
-
使用 cssSelector 和
selectByVisibleText()
:
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select#searchDropdownBox")))).selectByVisibleText("Books");
使用 xpath 和 selectByValue()
:
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@id='searchDropdownBox']")))).selectByValue("search-alias=stripbooks-intl-ship");
这篇关于尝试从Selenium Web Automation中的下拉菜单中选择一个选项-错误-"ElementNotInteractableException:无法滚动到视图中".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!