使用geb/selenium在页面上迭代

使用geb/selenium在页面上迭代,selenium,groovy,geb,Selenium,Groovy,Geb,我想遍历一个页面列表,验证每个页面的内容 对于GEB,我想迭代一下,例如https://github.com/trending 然后访问trending中的每一页并验证标题,然后继续下一页 但是,错误org.openqa.selenium.StaleElementReferenceException:stale元素引用:元素未附加到页面文档困扰着我 @Grapes([ @Grab('org.gebish:geb-core:3.3'), @Grab('org.seleniumhq.

我想遍历一个页面列表,验证每个页面的内容

对于GEB,我想迭代一下,例如
https://github.com/trending
然后访问trending中的每一页并验证标题,然后继续下一页

但是,错误
org.openqa.selenium.StaleElementReferenceException:stale元素引用:元素未附加到页面文档
困扰着我

@Grapes([
    @Grab('org.gebish:geb-core:3.3'),
    @Grab('org.seleniumhq.selenium:selenium-support:3.141.59'),
    @Grab('org.seleniumhq.selenium:selenium-chrome-driver:3.141.59'),
    @GrabExclude('org.codehaus.groovy:groovy-all:2.5.9')])
import geb.Browser
import geb.navigator.Navigator
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver

import geb.Page

import static geb.Browser.drive

System.setProperty("webdriver.chrome.driver","/Users/v/Downloads/chromedriver")
def chromeDriver = new ChromeDriver()
println chromeDriver.getSessionId()
drive(driver: chromeDriver, baseUrl: "https://github.com") { 
    to ProviderListPage
    Navigator pages = list()
    pages.each {
        to ProviderPage, it.attr("href").split('/').reverse()[1],it.attr("href").split('/').reverse()[0]
        waitFor { 5 }
        driver.navigate().back()
    }
    driver.quit()
}

class ProviderListPage extends Page {
    static url = "/trending"

    static content = {
        providers { $(".h3 a") }
    }
    def list() {
        return providers
    }

}
class ProviderPage extends Page {
    static content = {
        heading { $(".h3 a").text() }
    }
    def waitForHeading() {
        waitFor { assert $(".h3 a") }
    }
}

这是我为大家树立的榜样。仅更改
webdriver.chrome.driver

我知道页面从
ListPage
更改为
ProviderPage#1
,并导致错误:
StaleElementReferenceException
。但我不清楚的是,我如何能够在页面之间来回导航并浏览我的页面列表,例如,
ProviderPage\2

问题是:

  • 从概览页面导航到另一个页面,然后使用“返回浏览器”,即再次加载概览页面(即使可能从浏览器缓存)
  • 但您仍然是指第一次加载概述页面时的导航器,通过
    每个
    对其进行迭代。这意味着元素已经过时了
相反,您应该在第一次打开overview页面时获得遍历列表所需的所有信息。这也将节省时间,并使“浏览器返回”完全多余。这对我很有用:

package de.scrum\u master.stackoverflow.q59958656
导入geb.spock.GebReportingSpec
类GitHubTrendingIT扩展了GebReportingSpec{
def测试(){
鉴于:
browser.baseUrl=”https://github.com"
def providerListPage=至providerListPage
报告“提供者列表页”
providerListPage
.list()
.收集{
def pageInfo=it.attr(“href”).split('/').reverse()
[pageInfo[1],pageInfo[0]]
}
.每个{
println“趋势页=$it”
要创建ProviderPage,请单击它[0],单击它[1]
报告“趋势页面”
}
期望:
真的
}
}
我将代码包装到Groovy测试类中。如果想从脚本中执行代码,只需删除不需要的部分(我从来没有这样做过)

顺便说一句,在撰写本文时,测试打印以下内容:

trending page=[wuhan2020,WebApp]
趋势页面=[oldboyxx,jira_clone]
趋势页面=[wuhan2020,wuhan2020]
趋势页面=[microsoft,ApplicationInspector]
趋势页面=[wuhan2020,api服务器]
趋势页面=[lispczz,肺炎]
趋势页面=[sundowndev,黑客路线图]
趋势页面=[binhnguyennus,极好的可伸缩性]
趋势页面=[Puppeter,Puppeter]
趋势页面=[nicrusso7,雷克斯健身房]
趋势页面=[smicallef,spiderfoot]
趋势页面=[Willmogugan,rich]
趋势页面=[SwiftDocOrg,swift doc]
趋势页面=[迂回,脚本]
趋势页面=[全球公民,2019年武汉冠状病毒数据]
趋势页面=[sam hosseini,芬兰自由职业者]
趋势页面=[hzwer,shareOI]
趋势页面=[sebastianruder,NLP进度]
趋势页面=[giswqs,土工py笔记本]
趋势页面=[aamini,介绍深入学习]
趋势页=[Kethku,neovide]
趋势页面=[redcanaryco,原子红色团队]
趋势页面=[baowenbo,DAIN]
趋势页面=[Joey Castillo,打开的书]
趋势页面=[meik97,XSpotify]

共享一个完整的测试和页面类如何?谁能知道您为哪个元素获得了
StaleElementReferenceException
?请让您的问题重现。下次请在更新您的问题后通知我并发表评论。我只是碰巧注意到了。