【问题标题】:Element is not longer attached to the DOM元素不再附加到 DOM
【发布时间】:2017-09-25 09:27:01
【问题描述】:

我的目的是创建 elemnets 列表,然后单击它。对于第一个迭代循环完美运行,它打开第一个链接。然后失败了。

我的代码是:

    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

【问题讨论】:

  • 您是否意识到,如果您单击链接,将会加载一个新页面,并且页面上会发生一些变化,并且页面上可能不存在链接?怎么回事??
  • @KDD 你试过??????

标签: python selenium selenium-webdriver


【解决方案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')]"));
    }   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 2014-06-06
    • 2014-10-05
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多