Java 无法从firefox上的dropdpwn菜单中进行选择
我正在使用Java和Selenium编写一个测试。我有一个下拉菜单,我需要从中选择一些东西。这是我的代码:Java 无法从firefox上的dropdpwn菜单中进行选择,java,selenium,selenium-webdriver,selenium-chromedriver,selenium-firefoxdriver,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,Selenium Firefoxdriver,我正在使用Java和Selenium编写一个测试。我有一个下拉菜单,我需要从中选择一些东西。这是我的代码: Select s= new Select(driver.findElement(By.xpath("blabla"))); s.selectByVisibleText("theName"); 它可以在Chrome上运行,但在Firefox 47上,我遇到以下错误: org.openqa.selenium.ElementNotVisibleException: Element is
Select s= new Select(driver.findElement(By.xpath("blabla")));
s.selectByVisibleText("theName");
它可以在Chrome上运行,但在Firefox 47上,我遇到以下错误:
org.openqa.selenium.ElementNotVisibleException:
Element is not currently visible and so may not be interacted with
我知道如何通过其他方式从下拉菜单中进行选择,但我需要使用
选择对象。使用fluent wait to wait元素,chrome更快:
public static void waitUntilElementIsVisible(WebElement element, WebDriver driver)
{
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
wait.withTimeout(2, TimeUnit.MINUTES);
wait.ignoring(ElementNotVisibleException.class); //make sure that this exception is ignored
Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
System.out.println("Checking for the element!!");
if(element.isDisplayed() != true)
{
System.out.println("Target element is not visible");
}
return element;
}
};
wait.until(function);
}
使用fluent wait to wait元素,chrome更快:
public static void waitUntilElementIsVisible(WebElement element, WebDriver driver)
{
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
wait.withTimeout(2, TimeUnit.MINUTES);
wait.ignoring(ElementNotVisibleException.class); //make sure that this exception is ignored
Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
System.out.println("Checking for the element!!");
if(element.isDisplayed() != true)
{
System.out.println("Target element is not visible");
}
return element;
}
};
wait.until(function);
}
您可以使用WebDriverWait
类等待元素的可见性,如下所示:
WebDriverWait wait = new WebDriverWait(driver, customTime);
WebDriver driver = new FirefoxDriver();
Select s = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("blabla")));
s.selectByVisibleText("theName");
您可以使用WebDriverWait
类等待元素的可见性,如下所示:
WebDriverWait wait = new WebDriverWait(driver, customTime);
WebDriver driver = new FirefoxDriver();
Select s = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("blabla")));
s.selectByVisibleText("theName");
您是否验证了XPath在FF上有效?如果没有相关的HTML,很难提供帮助。因此,您是否使用WebDriverWait
?尝试CSS选择器而不是xpath,直到它变得可见。因为xpath在所有浏览器上可能都不相同。@Jeffs是的,xpath在FF和Chrome上是相同的,我们还使用了wait.unit(…visibilityOfElementLocatedBy(…)
To是否验证了xpath在FF上有效?如果没有相关的HTML,很难提供帮助。因此,您是否使用WebDriverWait
?尝试CSS选择器而不是xpath,直到它变得可见。因为xpath可能在所有浏览器上都不相同。@Jeffs是的,FF和Chrome的xpath是相同的,我们还使用了wait.unit(…visibilityOfElementLocatedBy(…)