Internet Explorer: check if element is visible

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Internet Explorer: check if element is visible

23 Nov 2017, 15:59

I have a script where I'm trying to remove the bar at the top of Google Translate in Internet Explorer, by clicking a certain button. The script works, but it toggles, rather than hides, so I'm looking for a way to determine whether the element is hidden.

I saw some ideas here but I'm not sure how to/if I can use them with AutoHotkey:
javascript - Check if element is visible in DOM - Stack Overflow
https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom

Checking the element's position would be a workaround, but I'm looking for a better way. Sometimes some elements' widths/heights are set to 0 when hidden, but AFAIK not in this case.

Thanks for reading.

Code: Select all

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

;e.g.
;Google Translate
;https://translate.google.com/translate?sl=de&tl=en&js=y&prev=_t&hl=en&ie=UTF-8&u=https://autohotkey.com/boards/viewtopic.php?f=11%26t=9798&edit-text=&act=url
;original link:
;'Echte' Arrays - Grundlagen - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=11&t=9798

q:: ;google translate - toggle hide/show bar, focus frame
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
oWB.document.getElementById("clp-btn").click()
;IID_IHTMLWindow2 := "{332C4427-26CB-11D0-B483-00C04FD90119}"
;VT_DISPATCH := 9
oDoc := ComObject(9, ComObjQuery(oWB.document.parentWindow.frames[0], "{332C4427-26CB-11D0-B483-00C04FD90119}", "{332C4427-26CB-11D0-B483-00C04FD90119}"), 1).document
oDoc.documentElement.focus()
oWB := oDoc := ""
return
[EDIT:]
- It may be that the control isn't hidden, and that it is has been pushed above the visible area. Is that common? I could check whether certain position values are negative or not.

Code: Select all

;before:
oWB.document.getElementById("clp-btn").click()

;after:
oElt := oWB.document.getElementById("gt-appbar")
if (oElt.getBoundingClientRect().top > 0)
	oWB.document.getElementById("clp-btn").click()
[EDIT:]
- One approach in this case would be to query the button that I send a click to, which has either an up arrow or down arrow appearance.

Code: Select all

;before:
oWB.document.getElementById("clp-btn").click()

;after:
if !InStr(oWB.document.getElementById("clp-btn").className, "clp")
	oWB.document.getElementById("clp-btn").click()
Last edited by jeeswg on 06 Feb 2019, 14:34, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Internet Explorer: check if element is visible

24 Nov 2017, 21:43

You definitely can do this. jQuery usually handles it pretty well and majority of websites use it. So you may be able to invoke it from AHK. https://api.jquery.com/visible-selector/
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: check if element is visible

06 Feb 2019, 12:57

- @kczx3: Thanks for the link, unfortunately 'visible'/'hidden' did not seem to work on Internet Explorer, although 'enabled'/'disabled' did. I tested this on Google's homepage.

Code: Select all

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

q:: ;internet explorer - test querySelectorAll
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)

vNum1 := oWB.document.all.length
vNum2 := oWB.document.querySelectorAll("input:enabled").length
vNum3 := oWB.document.querySelectorAll("input:disabled").length
vNum4 := "ERROR"
vNum5 := "ERROR"
try vNum4 := oWB.document.querySelectorAll("input:visible").length
try vNum5 := oWB.document.querySelectorAll("input:hidden").length
oWB := ""

MsgBox, % Format("{} {} {} {} {}", vNum1, vNum2, vNum3, vNum4, vNum5)
return
- Does anyone else have any other ideas for checking whether an element is visible/hidden?
- Also, is it possible to make an element hidden?
Last edited by jeeswg on 06 Feb 2019, 14:34, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4330
Joined: 29 Mar 2015, 09:41
Contact:

Re: Internet Explorer: check if element is visible

06 Feb 2019, 13:47

jeeswg wrote:
23 Nov 2017, 15:59
- It may be that the control isn't hidden, and that it is has been pushed above the visible area.
Exactly.

Code: Select all

style := oWB.document.getElementById("wtgbr").style
marginTop := style.marginTop
b := marginTop == "0px"
MsgBox, % b ? "visible" : "hidden"
if b
   style.display := "none" ; but that's not what you expected
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: check if element is visible

06 Feb 2019, 14:22

- Re. checking visibility.
- I tried MsgBox, % oElt.style.visibility but it didn't work on the webpage I tested.

- Re. making an element hidden.
- One workaround is to resize elements, to make them 'hidden'.
- Use the following script with caution.

Code: Select all

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

;WARNING: this will resize every element on the current webpage
q:: ;internet explorer - resize all elements
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)

oElts := oWB.document.all
Loop, % oElts.length
{
	oElt := oElts[A_Index-1]
	oElt.style.width := "0px"
	oElt.style.height := "0px"
}

oWB := ""
MsgBox, % "done"
return
[EDIT:]
- Here's a better solution to hide/show elements.

Code: Select all

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

;WARNING: some previously hidden elements may become shown
q:: ;internet explorer - hide and show all elements
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)

oElts := oWB.document.all
Loop, % oElts.length
{
	oElt := oElts[A_Index-1]
	oElt.style.visibility := "hidden"
}

Sleep, 2000
Loop, % oElts.length
{
	oElt := oElts[A_Index-1]
	oElt.style.visibility := "visible"
}

oWB := ""
MsgBox, % "done"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, Chunjee, downstairs, Frogrammer and 327 guests