2017-09-25 79 views
-2

我的目的是創建elemnets列表,然後單擊它。對於第一個迭代循環完美地工作,它打開第一個鏈接。然後失敗了。元素不再連接到DOM

我的代碼是:

hrefList = driver.find_elements_by_xpath(".//a[contains(text(), 'כרטיס רופא')]") 

    print("length of List is : " + str(len(hrefList))) 

    for href in hrefList: 
     print(href) 
     href.click() 

我得到一個錯誤信息:

selenium.common.exceptions.StaleElementReferenceException: 
Message: The element reference of <a> stale: either the element is no longer attached to the DOM or the page has been refreshed 
+0

你知道,如果你點擊鏈接在新頁面上會被加載,頁面上的內容會發生變化,頁面上可能不存在鏈接?情況如何? –

+0

@KDD U已嘗試??????? – iamsankalp89

回答

1

此錯誤來當元素不存在於DOM。你必須回到主界面,然後點擊

試試這個代碼,你會得到一個解決方案,這是Java代碼

String URL="https://www.ida.org.il/?pageType=19&langId=1&paramIds=%2Con_321%2Con_322%2Con_354%2Con_355%2Con_320&scope=&parameterSearch="; 
    WebDriver driver=new FirefoxDriver(); 
    driver.get(URL); 
    List<WebElement> links=driver.findElements(By.xpath("//a[contains(@href, 'https://www.ida.org.il/?categoryId=96318&itemId')]")); 
    System.out.println("Total links: "+links.size()); 
    for (int i=0;i<links.size();i++) { 


    links.get(i).click(); 
    System.out.println(i 
      +"Current URL is: "+driver.getCurrentUrl()); 
    driver.navigate().back(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    links=driver.findElements(By.xpath("//a[contains(@href, 'https://www.ida.org.il/?categoryId=96318&itemId')]")); 
    } 
+0

謝謝,請在勾上箭頭時加註 – iamsankalp89

相關問題