views:

1228

answers:

2

I'm using selenium RC and want to get all the attributes and all. Something like:

link = sel.get_full_link('//a[@id="specific-link"]')

and the result would of:

print link

would be:

<a id="specific-link" name="links-name" href="url"> text </a>

Is this possible?

thanks

+1  A: 

I think the best way to do this would be to use the getHtmlSource command to get the entire HTML source, and then use either a regular expression or HTML parser to extract the element of interest.

The following Java example will output all links to System.out:

selenium.open("http://www.example.com/");
String htmlSource = selenium.getHtmlSource();
Pattern linkElementPattern = Pattern.compile("<a\\b[^>]*href=\"[^>]*>(.*?)</a>");
Matcher linkElementMatcher = linkElementPattern.matcher(htmlSource);
while (linkElementMatcher.find()) {
    System.out.println(linkElementMatcher.group());
}
Dave Hunt
ya, I thought of that. I was hoping selenium would offer a more elegant solution.
Guy
Unfortunately Selenium goes not have a way to do this itself.
Dave Hunt
A: 

If the link isn't dynamic, then try this rather cheesy, hacky solution (This is in Python):

selenium.click("//a[text()='Link Text']")
selenium.wait_for_page_to_load(30000)
myurl = selenium.get_location()

Cheesy but it works.

David
I want to play around with the "source" of the link. Like see that the name is so and so and that there is no `<b>` tags inside the link data.. that sort of stuff. What you're suggesting will only give me the url which I can get: `selenium.get_attribute("//a[text()='Link Text']/@href")`
Guy