0

There is a dropdown list where each selection has a different URL under the dropdown buttons. Suppose when I select first option then it shows 10 hyperlink and select the second option it shows 5 hyperlinks, etc.

Problem - When I select the second option, it is still showing 10 hyperlinks instead of 5 and shows

org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

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());
        }
    }
}
JeffC
  • 22,180
  • 5
  • 32
  • 55
sandy
  • 1
  • 1

2 Answers2

0

where you selecting the element ?? (C# syntax example)

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

Community
  • 1
  • 1
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
0

I spent a little time cleaning up your code and added a few things. See if this works. As Leon said, I think one of the issues was that you didn't have code that actually changed the selected option.

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());
        }
    }
}

Suggestion: It might be useful to you to add the selected option text to your exception message.

JeffC
  • 22,180
  • 5
  • 32
  • 55