Reputation: 17268
@Grapes([
@Grab("org.codehaus.geb:geb-core:0.7.2"),
@Grab("org.seleniumhq.selenium:selenium-firefox-driver:2.25.0"),
@Grab("org.seleniumhq.selenium:selenium-support:2.25.0")
])
import geb.Browser
import org.openqa.selenium.firefox.FirefoxDriver
Browser.drive {
go "http://www.asu.edu/"
$("li a").each { a ->
println(a.text())
}
}
I'm trying to grab all links(as represented by a
tag) from web pages using the Geb
framework. But I failed to capture those links in dropdown lists. Take this page for example, I was not able to capture dropdown lists under Colleges & Schools
and Map & Locations
. In the output of the code above, each item in the dropdown lists was "an empty string" occupying one line. The beginning part of the output is as follows:
SIGN IN
ASU Home
My ASU
Colleges & Schools
Map & Locations
Contact ASU
Freshman
Transfer
Graduate
International
Military | Veteran
Home-Educated
Online
Prospective Students
Upvotes: 2
Views: 1967
Reputation: 6954
This is because you can only get text of elements that are visible. This is so that text() returns only text that is visble to the user.
Looks like that dropdown is styled to open when :hover pseudoclass is applied to it. There is drag and drop support in Geb and moveToElement action you could use to point the cursor to the element but it doesn't seem to work - I found some thread suggesting that Firefox driver doesn't handle it well. Unfortunatelly it is not possible to simulate :hover currently with jQuery so your only option is to use Geb's jQuery integration to manualy make the element visible:
$('#asu_universal_nav li', 2).find('ul').jquery.show()
After that the text in your links should be returned when calling text(). Another option is to make the dropdown visible not only when :hover but also when a class is applied and add/remove that class in mouseenter/mouseleave handlers. But that's only possible if you can modify the app that's serving the page you're automating/testing.
Upvotes: 4