如何解决尝试从Selenium Web Automation中的下拉列表中选择一个选项-错误-“ ElementNotInteractableException:无法滚动到视图中”
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自动化,我试图单击下拉列表并选择选项之一。
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
的选项,您需要为elementToBeClickable()
引入WebDriverWait,并且可以使用以下任一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");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。