RDE Posted March 15, 2022 Share Posted March 15, 2022 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 More sharing options...
MattHiggs Posted March 15, 2022 Share Posted March 15, 2022 (edited) 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 March 15, 2022 by MattHiggs Link to comment Share on other sites More sharing options...
Danp2 Posted March 15, 2022 Share Posted March 15, 2022 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. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
RDE Posted March 17, 2022 Author Share Posted March 17, 2022 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 More sharing options...
Danp2 Posted March 17, 2022 Share Posted March 17, 2022 You likely need to perform more validation on the window handle before assuming that it is the correct one. Some ideas -- Check the window title Check the associated executable Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
InnI Posted March 17, 2022 Share Posted March 17, 2022 $hWindow = WinGetHandle("[RegexpTitle:^.+(Firefox|Chrome|Edge)$]") Link to comment Share on other sites More sharing options...
RDE Posted March 25, 2022 Author Share Posted March 25, 2022 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 More sharing options...
Nine Posted March 25, 2022 Share Posted March 25, 2022 Provide the code you are using, otherwise it is just a free guess... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now