[Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post your working scripts, libraries and tools for AHK v1.1 and older
LeonR22
Posts: 1
Joined: 06 Jun 2019, 05:36

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

06 Jun 2019, 05:39

@GeekDude
Thanks a lot for posting this! It has been very useful!
I do have a problem.. Whenever i open more than 6 chrome instances, i can not control the 7th instance of chrome. I am using different ports for all of the instances. Any idea what could be causing this?

Thnx!
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

06 Jun 2019, 09:39

I seem to remember something about the maximum number of WebSocket connections being 6 by default - but I don't know enough about websockets to know if this applies here, but I would guess that this is related: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=35117&p=279726#p279726

But do you really need so many chrome instances? You know that you can open additional tabs in already existing instances, if needed?

I think you can also have different so-called browser contexts (Google debug protocol docs:"Similar to an incognito profile but you can have more than one.") in one browser instance but I am not sure if this is possible with the current implementation of Chrome.ahk... I tried it some time ago, iirc, but couldn't get it to work (doesn't mean anything - I might try it again). Edit: I think I figured it out now.
bmilcs
Posts: 11
Joined: 27 Feb 2017, 13:54

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

06 Jun 2019, 10:26

Leon, I got around the issue by adding connects & disconnects to every function. Unless you need to have 6 scripts in action at once, you should just disconnect & reconnect as needed; it's been working like a charm for me for some time now. I can't thank GeekDude enough!

Question
https://www.amazon.com

Using manual timers and hotkeys, I was able to change the zip code & apply it. However, they must've changed the code and I'm hoping I can get a better solution for the following:

1. Click Deliver to... (Easy enough)
https://i.imgur.com/HT78GoE.png

2. Wait for pop-up, then click "Change" URL (How would I achieve this without the use of sleep?)
https://i.imgur.com/3XrXrZ3.png

3. Wait for Input Box to appear, change .value() (How do I wait for the input box to appear?)
https://i.imgur.com/AnXoHM4.png

PS: I'm a novice coder at best and don't practice it by trade. What exactly allows for web site's to do this kind of animation in the first place, without going to a fresh url? Is it javascript? css? html5? I'm at a loss.

EDIT: Why is this breaking my links? :(

[Mod edit: Links repaired.]
RinkaDink
Posts: 33
Joined: 03 Sep 2018, 14:52

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

29 Jun 2019, 18:40

Dear Geekdude,

I want to thank you and all for sharing your codes and helping all with it.

As a newbie to AHK, DOM usage and JS it would be great if anyone could give me some examples of probably basic stuff.
I tend to not asking to provide a ready script as I really want to learn to do it myself but it helps a lot to get a direction.

What would help me or maybe other more unexperienced users would be an example with very basic expressions.

May I ask if anyone could provide based on the AHK board starting page: https://www.autohotkey.com/boards/

1) Codeline just to Focus the Searchbox in the upper right . (DOM indicated the ID as = "keywords"
2) Codeline to insert text to the Searchbox in the upper right
3) Codeline to click the search button (class = "button button-search")

thanks!

Best regards
RinkaDink
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

02 Jul 2019, 13:50

Hey Rinkadink - have you looked at the examples included with the Chrome.ahk class? They should tell you what you need to know - but if you are still confused, let me know and i can work up a quick example for you if you think you still need it.

And thanks to Geekdude of course! Amazing work as always.
JaxD
Posts: 9
Joined: 28 Jul 2019, 01:21

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

03 Aug 2019, 06:20

hi,

is there a documetation/help file for this?
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

06 Aug 2019, 13:48

Hi, I'm using some site that periodically opens new tab. But when new tab is opening, Chrome is stealing the focus. And even if Chrome is minimized, it restores the window.

So is it possible to prevent Chrome from stealing focus? Or maybe there's way to launch Chrome hidden? I know there's AHK command WinHide - and it works until, again, new tab is opened.
bmilcs
Posts: 11
Joined: 27 Feb 2017, 13:54

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

19 Aug 2019, 14:10

I'm having trouble scraping a .aspx based site. It requires a log-in so URLDownloadToFile isn't an option. However, I am able to manually save the content of the page to a .html file.

How would I go about automating this via this wonderful resource?
jainsavan
Posts: 1
Joined: 27 Aug 2019, 13:47

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

27 Aug 2019, 13:57

Hi GeekDude, Firstly thanks a lot for providing a useful resource. I'm using it to automate many things in my systems. For one specific system,I'm unable to run PageInstance := ChromeInst.GetPage() it gives the error as websocket undefined. Have been trying to find solution from past 8hrs and haven't got to know how to resolve it. It should be a simple change in setting I guess but not sure how to do it. Attached the screenshot, could you please help me with it.

Thanks in advance.


Edit

Issue has been resolved. The issue was caused because Websockets are supported only from IE 10. Installed IE 11 and everything working fine now.
Thanks again for the lovely functionalities @GeekDude :)
Attachments
Untitled.png
Websocket_error
Untitled.png (26.41 KiB) Viewed 9040 times
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

09 Sep 2019, 13:55

A project ive been working on which is fully built around Geekdudes Chrome.ahk Class. Its amazing!

The interface is fully designed with HTML/CSS/Javascript. I use AHK to tie in Excel - where the data is populated from, as well as other resources like internal databases, config files, and do things like attach the pdf's to Emails automatically etc, etc for a fully automated order entry workflow.

I owe you a Beer my man! Thanks again! :clap: :clap:

https://imgur.com/a/pPM06JN
DanielToward13
Posts: 74
Joined: 18 May 2017, 10:56

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

31 Oct 2019, 01:39

Is there any built-in function to control multi tabs in a page? How to open two tabs in a page and inject JS to each tab?
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

31 Oct 2019, 01:59

DanielToward13 wrote:
31 Oct 2019, 01:39
Is there any built-in function to control multi tabs in a page? How to open two tabs in a page and inject JS to each tab?
Do you mean multiple pages/tabs in one browser?

Built-in function is relative... sure, you can do it via the chrome debugging protocol and Chrome.ahk. But are there high-level functions available in Chrome.ahk to do this ? Not really, but you can get around... do you have more details about what you want to do ("inject JS" is a quite abstract term)? Otherwise, I can show you a minimal example later.
DanielToward13
Posts: 74
Joined: 18 May 2017, 10:56

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

31 Oct 2019, 03:01

@gregster Yes, multiple pages/tabs in a browser.

One example would be to click on an element by dynamic Xpath as:

Code: Select all

function getElementByXpath(path) {{}return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;{}}

getElementByXpath("//*[@id='Container']/div/div/div[div/span[contains(text(),'123456789')]]/following-sibling::div/button").click();
I have to execute them on multiple tabs with different text (i.e. contains(text(),'123456789')). For the next step, It would be perfect if I can have 2 different callback functions for each tab to implement the equivalent of wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='someID']"))); in Chrome DevTools Protocol. I can't use Selenium or other tools because they are not supporting real human profile fingerprints.
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

31 Oct 2019, 04:20

Code: Select all

path := "//*[@id='Container']/div/div/div[div/span[contains(text(),'123456789')]]/following-sibling::div/button"
PageInst.Evaluate("document.evaluate(" . """" . path . """" . ", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();")
DanielToward13
Posts: 74
Joined: 18 May 2017, 10:56

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

31 Oct 2019, 05:18

@Xtra Thanks. How to execute it for tab1 and tab2?
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

31 Oct 2019, 05:19

Here is a simple example with two tabs - press F2 to trigger xpath-based clicks on both pages:

Code: Select all

#NoEnv
SetBatchLines, -1
SetTitleMatchMode 2
#Include Chrome.ahk				; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=42890

url1 := "https://example.com/"
url2 := "https://google.com/"

; --- Create a new headless Chrome instance ---
FileCreateDir, ChromeProfile										; creates a profile subdirectory in current folder
ChromeInst := new Chrome("ChromeProfile")
winwait, Chrome 	

; --- Connect to the page ---
if !(Page1 := ChromeInst.GetPage( , , fnCallBack))
{
	MsgBox, Could not retrieve page!
	ChromeInst.Kill()
}
else
	Page1.WaitForLoad()

Page1.Call("Page.navigate", {"url": url1})			; Navigate to url
Page1.WaitForLoad()

msgbox create new tab
Page1.Call("Target.createTarget", {url : url2 })
Page2 := ChromeInst.GetPageByURL(url2, , , fnCallback2)			
Page2.WaitForLoad()
return

F2::
Page1.evaluate("document.evaluate('/html/body/div/p[2]/a', document).iterateNext().click()")
xpath := "'//*[@id=""tsf""]/div[2]/div[1]/div[3]/center/input[2]'"
Page2.evaluate("document.evaluate(" xpath ", document).iterateNext().click()")
return

Esc::ExitApp

Edit: Concerning the next step...
ExpectedConditions.visibilityOfElementLocated is not really js, but selenium-specific, right ?! Well, this might need a bit of hard thinking... what is this doing exactly ?
Does it just mean, no style="display:none" attribute and width/height > 0 . Then, it should be possible with a callback (or a timer or a loop, it depends what you really need) - I'll have a go with it tomorrow.
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

31 Oct 2019, 11:53

Yes, I used that in my code. Was there a problem ? Or was this directed at someone else ?
Or just a general service link? :) anyway, thank you! ;)
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

31 Oct 2019, 12:12

ExpectedConditions.visibilityOfElementLocated is not really js, but selenium-specific, right ?! Well, this might need a bit of hard thinking... what is this doing exactly ?
It wasnt clear why it was mentioned so i linked what you used for reference.

Somewhat OT but here is info for ExpectedConditions.visibilityOfElementLocated
https://selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
visibilityOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page and visible.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 118 guests