• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

What DOM properties and methods can I use to click on this element?

I wish I could modify the DOM (add a class or an id to the element...) but I cannot.

I need to click on this element:

<a href="javascript:__doPostBack('Results$_ctl5$_ctl0','')" style="color:#003069;">Address2</a>

Open in new window


I know I can select all anchor elements with e.g.
arefs := document.getElementsByTagName("a")

Open in new window


Not sure if that is the easiest way to begin, or whether there is a better way.

Can someone please provide the code that will click on that href using DOM properties/methods?

Thanks-
Sam
0
SAbboushi
Asked:
SAbboushi
  • 4
  • 4
  • 2
1 Solution
 
Chris StanyonCommented:
Going to be difficult without context. You can select elements that are children of other elements, or based on position within a container element (first, last, nth child etc) so where in the DOM does this sit?

We need to see it 'in context' to give you an answer
0
 
COBOLdinosaurCommented:
Just add an id to the element and the you can access it with document.getElementById()

Cd&
0
 
Chris StanyonCommented:
@Cd&

I wish I could modify the DOM (add a class or an id to the element...) but I cannot.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
COBOLdinosaurCommented:
Yes I saw that and if they actually have such limited access that they cannot change a line of code then whoever or whatever is imposing that kind of restriction is obtuse, because you can do all kinds of damage in the DOM while at most you can only screw up a line of code if you have access.

@SAbboushi,

What we really need to know is what kind of nutty project puts you in a position where you cannot access the code but you have the ability to use scripting of the DOM that could literally destroy the page.

Cd&
0
 
SAbboushiAuthor Commented:
Maybe I have misled as a result of my "noobness"...

I do not have control over the webpage source; but I can change the DOM interactively by code in order to manipulate it (...if I said that correctly)  ; )

I wish I could modify the DOM (add a class or an id to the element...) but I cannot.

By this, I meant in the original html source; but if there is a way to add an id to the DOM after I have read the DOM, that would be good.

The href I want to click on is an element within a table:

TableHTML :=  document.getElementById("Table1").innerhtml

which contains e.g.

<tbody><tr>
		<td align="Center">&nbsp;</td><td><a href="javascript:__doPostBack('Results$_0','')" style="">Address</a></td><td align="Center"><a href="javascript:__doPostBack('Results$_1','')" style="">Owner</a></td><td align="Center"><a href="javascript:__doPostBack('Results$_2','')" style="">Price</a></td><td><a href="javascript:__doPostBack('Results$_3','')" style="">Bed</a></td><td><a href="javascript:__doPostBack('Results$_4','')" style="">Bath</a></td><td align="Center"><a href="javascript:__doPostBack('Results$_5','')" style="">SqFt</a></td>
	</tr><tr>
		<td align="Center"><input id="Results__ctl3" type="checkbox" name="Results:_ctl3" onclick="disableCheckBoxes(this.form);__doPostBack('Results$_ctl3$cbItem','')" language="javascript">
</td><td><a href="javascript:__doPostBack('Results$_ctl5$_ctl0','')">2707 RALPHO ST</a></td><td>JOHN SMITH</td><td align="Right">$237,416</td><td>04</td><td>02/1 </td><td align="Right">3,152</td>
	</tr>
</tbody>

Open in new window


In the above example, I want to click on: <a href="javascript:__doPostBack('Results$_ctl5$_ctl0','')">
0
 
Chris StanyonCommented:
As Cobol said, it does seem odd that you can inject javascript into a document but can't edit the HTML. There's no easy way of selecting that link because it has no unique identifier. You could do some complicated traversal to select it, but you'd need some consistency - i,e if it's always the last link in that table then you could use:

$('#Table1 a').last()

You could loop through every link and do a logical comparison against the href (but that's really messy)

Your best bet is to get an ID (or at least a Class) added to the A. If you can't get the HTML edited then you're probably out of luck on this one
0
 
SAbboushiAuthor Commented:
>> but can't edit the HTML

Let me make sure I understand: the webpage was created by another organization; we want to programatically navigate to an href on their page.  I would consider it unreasonable for me to contact another organization and ask them to change their website (i.e. add an id to an element) to accommodate our desire for an easy solution to have our code click on that link on their website.

That's what I mean by I cannot change the source HTML

>> There's no easy way of selecting that link because it has no unique identifier.
But "Results$_ctl5$_ctl0" is a unique identifier... or maybe there is a definition for "unique identifier" that I haven't yet learned...? I'm new a this  ; )
0
 
Chris StanyonCommented:
But "Results$_ctl5$_ctl0" is a unique identifier
No it's not. It's part of the href!

I'm not really following your logic on this one. It looks like you are wanting to use javascript to automatically click on a remote link. The whole concept is flawed - allowing javascript the ability to control the DOM of a remote page is a massive security concern and will just not work!

In order to select elements from the DOM you'll need to load the whole of the remote page into your script. You're then trying to 'click' a link from that page that has a javascript function as an href (bad design by the way). When you try and fire that click in your script it will fail because it will try and call a javascript function called __doPostBack() which you don't have!

I think you have to put this one to bed as a no-go. It's just not going to happen.
0
 
SAbboushiAuthor Commented:
Thanks for your post.  Seems you have misunderstood mine.
0
 
SAbboushiAuthor Commented:
k thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now