问题
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");
}
}
Code Explanation:
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");
Error:
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'
回答1:
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:
Using cssSelector and
selectByVisibleText()
:new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select#searchDropdownBox")))).selectByVisibleText("Books");
Using xpath and
selectByValue()
:new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@id='searchDropdownBox']")))).selectByValue("search-alias=stripbooks-intl-ship");
来源:https://stackoverflow.com/questions/63854405/trying-to-select-an-option-from-the-dropdown-in-selenium-web-automation-error