【问题标题】:How to select a value from a drop down menu如何从下拉菜单中选择一个值
【发布时间】:2017-01-08 20:57:46
【问题描述】:

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

我试过了:

    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)

【问题讨论】:

  • 请分享 html sn-p 以提供帮助!

标签: selenium selenium-webdriver drop-down-menu mouseover pageobjects


【解决方案1】:

所以您将鼠标悬停在某个元素上,然后尝试点击特定链接?您使用的是哪个驱动程序?你真的看到这个菜单在悬停后正确呈现了吗?

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

改用wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("iMacs"))); 试试看它是否会失败(以及在这种情况下会引发什么异常)。

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

【讨论】:

  • 同样的错误。我使用它是因为我不知道还能尝试什么。我是测试的初学者。您还有其他解决方案吗?
  • 好的,看看我的更新和这 3 个问题。对于单击,您可以使用 subElement.click() 而不是这 3 个操作行。
  • 我用的是chrome驱动。我的测试做任何等待使用的东西,而不是我说的,问题是有时跌倒测试,过去通过。
  • 如果你完全删除这行代码会发生什么:wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));?在这种情况下粘贴异常。
  • 当您删除代码行时:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“link text”,“selector”:“iMacs”} (会话信息:chrome=55.0.2883.87)(驱动程序信息:chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),平台=Windows NT 6.3.9600 x86_64)(警告:服务器未提供任何堆栈跟踪信息)
【解决方案2】:

请同时检查sendkeys 选项。可能是您的代码中的帮助。

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

【讨论】:

    【解决方案3】:

    我尝试在 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();
    

    上面的代码应该总是有效的。如果您还有任何疑问,请告诉我。

    【讨论】:

    • 我不需要点击下拉菜单,我只需要在菜单中移动。本站测试:store.demoqa.com.
    • 请尝试修改后的代码,如果能解决您的问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多