从下拉列表中选择第二个选项后,WebElement仍显示第一个选项的记录

时间:2016-06-25 21:06:45

标签: java selenium selenium-webdriver

有一个下拉列表,其中每个选项在下拉按钮下都有不同的URL。假设当我选择第一个选项然后它显示10个超链接并选择第二个选项它显示5个超链接等

问题 - 当我选择第二个选项时,它仍显示10个超链接而不是5个并显示

  

org.openqa.selenium.StaleElementReferenceException:找不到元素   在缓存中 - 也许页面自查找以来已经发生了变化

Select select = new Select(selectdropdown);
List<WebElement> options = select.getOptions();
int isize = options.size();

for (int i = 0; i < isize; i++)
{
    String value = select.getOptions().get(i).getText();
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    WebElement WebElementer = driver.findElement(By.xpath("//*[@id='content-inner']"));
    List<WebElement> elementList = new ArrayList<>();
    elementList = WebElementer.findElements(By.cssSelector("a[href]"));
    System.out.println("Total number of links found" + elementList.size());
    System.out.println("to check wheather link is working or not");
    for (WebElement element : elementList)
    {
        try
        {
            System.out.println("URL: " + element.getAttribute("href").trim() + " returned "
                + islinkBroken(new URL(element.getAttribute("href").trim())));
        }
        catch (Exception exp)
        {
            System.out.println("At " + element.getAttribute("innerHTML")
                + " Exception occured -&gt; " + exp.getMessage());
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你在哪里选择元素? (C#语法示例)

IList<IWebElement> accountsDDL = driver.FindElements(By.XPath("//select[@id='yourSelectId']/option"));

for (int i = 1; i < accountsDDL.Count; i++)
{
   new SelectElement(driver.FindElement(By.Name("yourSelectId"))).SelectByText(accountsDDL[i].Text); // Selecting the element
}

In java

答案 1 :(得分:0)

我花了一点时间清理代码并添加了一些东西。看看这是否有效。正如莱昂所说,我认为其中一个问题是您没有实际更改所选选项的代码。

Select select = new Select(selectdropdown);
for (int i = 0; i < select.getOptions().size(); i++)
{
    select.selectByIndex(i); // you were missing this line?
    // String value = select.getFirstSelectedOption().getText(); // this variable is never used
    // driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); // this doesn't do what you think it does
    // I think this next line should work. I combined the two locators into one.
    List<WebElement> elementList = driver.findElements(By.cssSelector("#content-inner a[href]"));
    System.out.println("Total number of links found" + elementList.size());
    System.out.println("to check wheather link is working or not");
    for (WebElement element : elementList)
    {
        try
        {
            String href = element.getAttribute("href").trim();
            System.out.println("URL: " + href + " returned " + islinkBroken(new URL(href)));
        }
        catch (Exception exp)
        {
            System.out.println("At " + element.getAttribute("innerHTML") + " Exception occured -&gt; " + exp.getMessage());
        }
    }
}

建议:将选定的选项文本添加到异常消息中可能很有用。