我想遍历页面列表,验证每个页面的内容
使用GEB,我想迭代遍历eg。然后,https://github.com/trending访问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
发布于 2020-01-30 10:42:27
问题是:
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]https://stackoverflow.com/questions/59958656
复制相似问题