gpt4 book ai didi

java - 无法从 Firefox 的下拉菜单中进行选择

转载 作者:太空宇宙 更新时间:2023-11-04 12:16:05 24 4
gpt4 key购买 nike

我正在使用JavaSelenium来编写测试。我有一个下拉菜单,我需要从中选择一些内容。这是我的代码:

 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

我知道如何通过其他方式从下拉菜单中进行选择,但我需要使用 Select 对象。

最佳答案

使用Fluent 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);
}

然后你可以调用它:

WebElement el = driver.findElement(By.xpath("blabla"));
waitUntilElementIsVisible(el, driver);

关于java - 无法从 Firefox 的下拉菜单中进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39401038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com