Selenium 如何从下拉菜单中选择值

Selenium 如何从下拉菜单中选择值,selenium,selenium-webdriver,drop-down-menu,mouseover,pageobjects,Selenium,Selenium Webdriver,Drop Down Menu,Mouseover,Pageobjects,我想从下拉菜单中选择该选项。我尝试了很多方法,但都失败了 我试过: WebDriverWait wait = new WebDriverWait (driver, 5); wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs"))); WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(Ex

我想从下拉菜单中选择该选项。我尝试了很多方法,但都失败了

我试过:

    WebDriverWait wait = new WebDriverWait (driver, 5);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));

    WebDriverWait wait = new WebDriverWait(driver, 10); 
    wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("iMacs")));
    waitForElementToBeDisplayed(driver.findElement(By.linkText("iMacs")), 200);
    WebDriverWait wait = new WebDriverWait(driver, 30); 
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='iMacs']")));
这是我的代码:

            WebElement element = driver.findElement(By.linkText("Product Category"));
            Actions action = new Actions(driver);
            action.moveToElement(element).perform();
            WebDriverWait wait = new WebDriverWait (driver, 5);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
            WebElement subElement = driver.findElement(By.linkText("iMacs"));
            action.moveToElement(subElement);
            action.click();
            action.perform();
这是我的错误:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.linkText: iMacs (tried for 5 second(s) with 500 MILLISECONDS interval)

所以你在某个元素上悬停,然后试图点击特定的链接?你用的是哪个司机?你真的看到这个菜单在悬停后正确地呈现了吗

您的代码在
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(“iMacs”))上失败

尝试使用
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText(“iMacs”)),然后查看它是否失败(以及在这种情况下引发的异常)


你为什么要用Actions API来执行点击呢?

我试着用Chrome执行你的代码,它总是对我有用。然而,正如有时它会失败一样,您可以在代码中加入恢复机制,如下所示:

WebElement element = driver.findElement(By.linkText("Product Category"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebDriverWait wait = new WebDriverWait (driver, 5);
try {
       wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
} catch (WebDriverException we) {
       System.out.println("First attempt to wait for visibility of 'iMacs' failed. Retrying...");
       action.moveToElement(driver.findElement(By.linkText("Product Category"))).perform();
       wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
}
WebElement subElement = driver.findElement(By.linkText("iMacs"));
action.moveToElement(subElement);
action.click();
action.perform();

上述代码应始终有效。如果您还有任何疑问,请告诉我。

请同时检查
发送键
选项。这可能对您的代码有帮助

driver.findElement(By.xpath("code']")).sendKeys("testdata");

同样的错误。我使用这个是因为我不知道还有什么可以尝试。我是测试的初学者。你有其他的解决方案吗?好的,看看我的更新和这3个问题。对于单击,您可以使用
子元素。单击()
而不是这三条操作行。我使用chrome驱动程序。我的测试做的是等待使用,而不是我所说的,问题是有时秋天的测试会通过。如果完全删除这行代码会发生什么:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(“iMacs”)?粘贴在本例中得到的异常。删除代码行时:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“链接文本”,“选择器”:“iMacs”}(会话信息:chrome=55.0.2883.87)(驱动程序信息:chromedriver=2.25.426923(0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.3.9600 x86_64)(警告:服务器未提供任何stacktrace信息)请共享html代码片段以获得帮助!我不需要点击下拉菜单,我只需要在菜单中移动。此站点测试:。请尝试修改后的代码,如果它解决了您的问题,请告诉我。