我想遍历页面列表以验证每个页面的内容
[我想通过GEB进行迭代。https://github.com/trending
然后访问趋势中的每一页并验证标题,然后继续下一页。
但是错误org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
困扰着我。
@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
问题是:
each
在导航器上进行迭代。这意味着元素已经过时。相反,您应该在首次打开概述页面时获得在列表上进行迭代所需的所有信息。这还将节省时间,并使“浏览器返回”完全多余。这对我有用:
package de.scrum_master.stackoverflow.q59958656 import geb.spock.GebReportingSpec class GitHubTrendingIT extends GebReportingSpec { def test() { given: browser.baseUrl = "https://github.com" def providerListPage = to ProviderListPage report "provider list page" providerListPage .list() .collect { def pageInfo = it.attr("href").split('/').reverse() [pageInfo[1], pageInfo[0]] } .each { println "trending page = $it" to ProviderPage, it[0], it[1] report "trending page" } expect: true } }
我将代码包装到Groovy测试类中。如果要从脚本执行代码,只需删除不需要的部分(我从不这样做)。
顺便说一句,在编写本文时,测试打印以下内容:
trending page = [wuhan2020, WebApp]
trending page = [oldboyxx, jira_clone]
trending page = [wuhan2020, wuhan2020]
trending page = [microsoft, ApplicationInspector]
trending page = [wuhan2020, api-server]
trending page = [lispczz, pneumonia]
trending page = [sundowndev, hacker-roadmap]
trending page = [binhnguyennus, awesome-scalability]
trending page = [puppeteer, puppeteer]
trending page = [nicrusso7, rex-gym]
trending page = [smicallef, spiderfoot]
trending page = [willmcgugan, rich]
trending page = [SwiftDocOrg, swift-doc]
trending page = [outflanknl, Scripts]
trending page = [globalcitizen, 2019-wuhan-coronavirus-data]
trending page = [sam-hosseini, freelancing-in-finland]
trending page = [hzwer, shareOI]
trending page = [sebastianruder, NLP-progress]
trending page = [giswqs, earthengine-py-notebooks]
trending page = [aamini, introtodeeplearning]
trending page = [Kethku, neovide]
trending page = [redcanaryco, atomic-red-team]
trending page = [baowenbo, DAIN]
trending page = [joeycastillo, The-Open-Book]
trending page = [meik97, XSpotify]