How to handle & in href when using getattribute() in selenium

325 views
Skip to first unread message

Raghuram

unread,
Jul 4, 2012, 7:59:01 AM7/4/12
to seleniu...@googlegroups.com

I need to get the complete href link.

This is My HTML code.
<div class="UIStoryAttachment_Title">
     i=23771a5c40554257614b6b81bf58a336" />
</div>

System.out.println("URL : "+webdirver.findElementByCssSelector("div.UIStoryAttachment_Title a").getAttribute("href");

when print this, am getting

Can someone help with this.

Thanks
Raghuram

sai kiran nukala

unread,
Jul 5, 2012, 3:02:05 AM7/5/12
to seleniu...@googlegroups.com
You are getting the Link, what do you mean by  "complete href link. " ? When you do getAttribute("attributeName") it will return attributeValue.

Raghuram

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/YWMagCh6ZpIJ.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.



--
With Regards,
Sai Kiran

David

unread,
Jul 5, 2012, 2:58:53 PM7/5/12
to seleniu...@googlegroups.com
Sai,

Raghuram stated that when he got the attribute from Selenium, the value is truncated, as everything including and after the ampersand "&" character is missing. If you look closely at the given HTML, you see that the href attribute is supposed to be longer than what is returned by Selenium, hopefully you noticed that the long text wrapped into next line and Google Groups rendered only part of the href into a clickable link in this post, with the rest of the link as text, hate when that happens when you copy & paste text, and how HTML view source is rendered sometimes.

On Thursday, July 5, 2012 12:02:05 AM UTC-7, sai kiran nukala wrote:
You are getting the Link, what do you mean by  "complete href link. " ? When you do getAttribute("attributeName") it will return attributeValue.

On Wed, Jul 4, 2012 at 5:29 PM, Raghuram <raghu...@gmail.com> wrote:

I need to get the complete href link.

This is My HTML code.
<div class="UIStoryAttachment_Title">
     i=23771a5c40554257614b6b81bf58a336" />
</div>

System.out.println("URL : "+webdirver.findElementByCssSelector("div.UIStoryAttachment_Title a").getAttribute("href");

when print this, am getting

Can someone help with this.

Thanks
Raghuram

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/YWMagCh6ZpIJ.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

sai kiran nukala

unread,
Jul 10, 2012, 3:41:02 AM7/10/12
to seleniu...@googlegroups.com
Thanks for the clarification David, so in such cases what are we supposed to do? any such exp.?

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/6Z4m31ytEeEJ.

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

David

unread,
Jul 10, 2012, 2:22:52 PM7/10/12
to seleniu...@googlegroups.com
I've never encountered this issue myself, so can't say from experience. I don't think there's much alternatives but one could try the following as workarounds. Neither is as simple as getting HREF attribute itself though.

* get the innerHTML attribute of the hyperlink's parent, the div element. This would have the source HTML for the link, which one can parse out the HREF attribute.

* get page source to parse out the HREF attribute. The innerHTML option is preferred so you deal with less text to parse, and in case page source is prerendered content. innerHTML may reflect AJAX DOM changes more appropriately but not sure.

Hopefully getting the HTML source you will get the content after the & character.

ankit

unread,
Jul 10, 2012, 2:36:12 PM7/10/12
to seleniu...@googlegroups.com
You could also use executeScript and use the jQuery attr() or prop() method if the website under test has included jQuery (or include it yourself)

David

unread,
Jul 10, 2012, 9:35:41 PM7/10/12
to seleniu...@googlegroups.com
In terms of use of jQuery as workaround, then use of the DOM itself may also be a possible workaround, which doesn't require jQuery.

Use of DOM though either requires use of document.getElementById("id") or similar. Or you might be able to pass in WebElement to the javascript to access it's DOM via javascript rather than use getAttribute. To see how to do that, see example in comment #60 in  http://code.google.com/p/selenium/issues/detail?id=2067.

once you have the element via DOM in JS, can just access href attribute as a property I believe like this simple example:

document.getElementBy("someLinkId").href //return that via javascript to store in a variable.
Reply all
Reply to author
Forward
0 new messages