Jump to content

Read URL of web page fails in Edge/Chrome


RDE
 Share

Recommended Posts

I wrote this snippet of code to read the URL of the current web page.

The first time it is run, it almost always fails under Edge or Chrome but always works when the code is run a second time.

It works first time, every time, when using Firefox.

Whilst the obvious work-around is to use Firefox, it grates! Is anyone able to point me in the right direction to fix this issue?

    ClipPut('')
    $hWindow = WinGetHandle("[CLASS:Chrome_WidgetWin_1]")
    If @error Then $hWindow = WinGetHandle("[CLASS:MozillaWindowClass]")
    _DebugOut('Window handle = ' & $hWindow)
    WinActivate($hWindow)
    WinWaitActive($hWindow)
    Send('!d')
    Send('^c')
    $sURL = ClipGet()
    _DebugOut('$sURL = ' & $sURL)
 

Link to comment
Share on other sites

I see you include some debug functions in the snippette.  Does the debug tell you anything?  The only suggestion that I can think of is to include a couple of brief "sleep" functions in between the other functions.  Give the environment time to actually process the key strokes and all that.

Edited by MattHiggs
Link to comment
Share on other sites

Pay attention to the window handle being returned. It appears that the incorrect handle is being returned when the function fails to return the URL. Also, be aware that there are other apps besides browsers that use Chrome_WidgetWin_1 for the window class. Therefore, this method could be impacted if one of those apps is active.

Link to comment
Share on other sites

Thanks for the response. Picking up on the window handle aspect, I tried this:

Re-boot the PC. Open Chrome with three tabs active: this page; my index page; one of my pages called from the index.as the active tab. Open AutoIT and run the code above.

Results
First run: Window handle = 0x000102A6, Correct URL returned

Next, Click on to my next page (from a "Next page" link). Second run: Window handle = 0x0003053E, URL null.

Third run (no change to browser): Window handle = 0x000102A6, Correct URL returned

This repeats multiple times. The Window handle on failed runs varies, the Window handle on successful runs is always (well, 7 or 8 repetitions anyway) 0x000102A6 for this session.

Once I have got a successful run, I get successful runs on the other tabs with the 0x000102A6 Window handle so it's definitely a Window handle problem. Although wasn't running any other program, Task Manager reports 73 Background processes.

Looks like I'll have to stick to Firefox :( 

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for the suggestions guys but I still get this weird inconsistency with Edge and Chrome. The window title is the same as the article's title so there is no consistent text that I can latch on to. The null URL is returned when/because the target window is not actually activated so I put a Do loop in to keep running the program until it returned a valid URL. It worked after multiple iterations (never less than 8, rarely more than 15) but took a silly amount of time to get there.

I use it to load the html into my text editor when minor edits to a page are required so it's no problem being forced to use Firefox - just weird!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...