2016-06-25 83 views
0

有一個下拉列表,其中每個選擇在下拉按鈕下具有不同的URL。假設當我選擇第一選項,則顯示超鏈接10並選擇它顯示5頁的超鏈接的第二個選項,等等從下拉菜單中選擇第二個選項後,WebElement仍顯示第一個選項的記錄

問題 - 當我選擇第二個選項,它仍顯示出代替5- 10個超鏈接和顯示

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

0

我花了一點時間清理你的代碼,並添加了一些東西。看看這是否有效。正如Leon所說,我認爲其中一個問題是您沒有真正改變選定選項的代碼。

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

建議:將選定的選項文本添加到異常消息中可能很有用。

相關問題