Click link contained in <div>

Get help with using AutoHotkey and its commands and hotkeys
ecksphore
Posts: 33
Joined: 03 Feb 2015, 22:36

Click link contained in <div>

Post by ecksphore » 29 Sep 2015, 23:23

I'm having a heck of a time trying to click on a link in a <div>. The reason I don't look just for "a" tags, is that I am only interested links that are contained in a div - the other 100+ links I want to ignore.

Any help would be appreciated.

<div><a href="http://www.somelink.com">all-time today</a></div>
<div><a href="http://www.somelink.com">all-time yesterday</a></div>

Code: Select all

links := WB.document.all.tags("div").length
loop %links%
{
	link := WB.document.all.tags("div")[A_Index-1].innerHTML
	If link contains all-time
	{
		WB.document.all.tags("div")[A_Index-1].click() ; <---- this is where I'm not sure what to do
	}
}
User avatar
Blackholyman
Posts: 1271
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

Re: Click link contained in <div>

Post by Blackholyman » 30 Sep 2015, 00:38

Example (untested)

Code: Select all

divs := wb.document.getElementsByTagName("div")
loop % divs.length
{
	innerText := divs[A_Index-1].innerText
	If instr(innerText, "all-time")
	{
		divs[A_Index-1].getElementsByTagName("A")[0].click()
		break
	}
}
Also check out:
[hr][/hr]The Monthly AutoHotkey Webinars

My Autohotkey Blog
:dance: [hr][/hr]
User avatar
sinkfaze
Posts: 411
Joined: 01 Oct 2013, 08:01

Re: Click link contained in <div>

Post by sinkfaze » 30 Sep 2015, 07:53

Why would you need to process that by <div> tags at all? I would think looping through the link collection would work just fine:

Code: Select all

links := WB.document.links
loop %   links.length
{}   Until InStr(links[(i := A_Index-1)].innerText,"all-time")
links[i].click()
User avatar
Blackholyman
Posts: 1271
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

Re: Click link contained in <div>

Post by Blackholyman » 30 Sep 2015, 08:03

@sinkfaze what if there are other links with the text "all-time" but in link elements that are not child elements of a div element?
Also check out:
[hr][/hr]The Monthly AutoHotkey Webinars

My Autohotkey Blog
:dance: [hr][/hr]
User avatar
sinkfaze
Posts: 411
Joined: 01 Oct 2013, 08:01

Re: Click link contained in <div>

Post by sinkfaze » 30 Sep 2015, 09:46

What if there are other div elements which contain the inner text "all-time"?

The only caveat the OP has given is this:
The reason I don't look just for "a" tags, is that I am only interested links that are contained in a div - the other 100+ links I want to ignore.
That's saying to me the <div> tag is significant only for the purpose of finding the child link. In my experience, you are typically going to run into as many or more div elements on a webpage as you are links anyway, so you can either search hundreds of div elements, or hundreds of links to get to the same result. You may as well go straight to the source.
User avatar
Blackholyman
Posts: 1271
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

Re: Click link contained in <div>

Post by Blackholyman » 30 Sep 2015, 10:28

For sure I'm only saying that he may have html like this

Code: Select all

<a href="http://www.somelink.com">all-time this year</a>
<a href="http://www.somelink.com">all-time last year</a>
<div><a href="http://www.somelink.com">all-time today</a></div>
<div><a href="http://www.somelink.com">all-time yesterday</a></div>
Now when I look at my own example it will not even work right...

maybe something like this

Code: Select all

loop % (links := wb.document.links).length {
	if (InStr(links[(i := A_Index-1)].innerText,"all-time")) And (links[(i := A_Index-1)].parentNode.tagName = "div")
		break
}   
links[i].click()
Also check out:
[hr][/hr]The Monthly AutoHotkey Webinars

My Autohotkey Blog
:dance: [hr][/hr]
User avatar
Lucianoc
Posts: 10
Joined: 02 Oct 2015, 01:21

Re: Click link contained in <div>

Post by Lucianoc » 02 Oct 2015, 06:10

WB.document.all.tags
is this IExplorer COM Object?

assuming it follows JS properties

instead of innerHTML use ChildElement or ChiuldNode[x] (array i think), test if child is A, check its HREF and INNER(which for <A> is the highlighted text)

theres also NextChild or NextSibling (property), then you would start from DIV's firstChildNode

(doublecheck these)
Post Reply

Return to “Ask For Help”