How to get the full source of a link using selenium
-
07-07-2019 - |
Question
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
Solution
Here's a fancier solution:
sel.get_eval("window.document.getElementByID('ID').innerHTML")
(don't be picky with me on the javascript..)
OTHER TIPS
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());
}
String href = selenium.getAttribute("xpath=//a[@id="specific-link"]/@href")
I've been trying to do just this, and came up with the following:-
var selenium = Selenium;
string linkText = selenium.GetText("//a[@href='/admin/design-management']");
Assert.AreEqual("Design Management", linkText);
use below code to get all the links on the page:
$str3= "window.document.getElementsByTagName('a')";
$k = $this->selenium->getEval($str3);
$url = explode(",",$k);
$array_size = count($url);
$name=array();
$l=0;
for($i=0;$i<$array_size;$i++)
{
if(!strstr($url[$i], 'javascript'))
{
$name[$l]=$url[$i];
echo "\n".$name[$l];
$l++;
}
}
If the link isn't dynamic, then try this rather cheesy, hacky solution (This is in Python):
selenium.click("//a[text()='Link Text']")<br>
selenium.wait_for_page_to_load(30000)<br>
myurl = selenium.get_location()
Cheesy but it works.
Note: this will not work if the link redirects.