This is my first question here in Stack Overflow, so here is the problem.
In one of my Python program which I use to automate certain processes with Selenium with a chrome browser, there's a line that goes like this...
for i in range(a): # a is just an integer variable defined somewhere before this line
driver.switch_to.window(driver.window_handles[i + 1])
driver.find_element(By.CLASS_NAME, "saveButton").click()
Now, for anyone familiar with this, you would expect that immediately after the ".click()", the for loop will continue for the next integer value which causes the next operation to be done on the next window handle, so on and so forth. However, due to a certain reason that I do not understand, the program kind of stops at the ".click()" line, wait for the page to be fully loaded, and only then continues with the next window handle. Now this will continue over and over again until the for loop above is complete.
I've tried a lot of thing to fix this issue
With Asyncio
I noticed that Asyncio has its own built in syntax, "asyncio.sleep()" that demonstrate a running function that is waiting for something, and using other syntaxes within the module, we can skip the waiting function and let the program run without waiting for the function to finish. However, the ".click()" does not work the same way as "asyncio.sleep()", it kind of work the same way as "time.sleep()" and correct me if I am wrong that Asyncio cannot skip "time.sleep()" as it block the program completely until it is finished. The same thing happened when I tried Asyncio to skip the pause when the ".click()" is performed, but it didn't work.
With Multiprocessing
I tried to skip the waiting due to ".click()" with Multiprocessing. The problem with Multiprocessing for this context is that when you put blocks of code under "name = main", it will open new browser for each "name = main" generated from the for loop which ends up using lots of processing power.
With Threading
I don't remember what I exactly have done with threading, the only thing that I remember is that my approach with threading didn't work.
With "driver.set_page_load_timeout()"
This is a Selenium syntax that raises an exception when a page loads longer than the specified time. I tried to use this with "Try" and "Except" and it almost work! Here how it looks like:
for i in range(a): driver.set_page_load_timeout(1) try: driver.find_element(By.CLASS_NAME, "saveButton").click() except: driver.switch_to.window(driver.window_handles[i + 1])The idea here is that after 1 second the ".click()" has been performed, the program will proceed to execute the "except" code which is to switch to the next window handle since timeout will be raised when the page hits the 1 second mark to load. And this does switch to the next window handle! However, the only problem with this is that "driver.set_page_load_timeout()" does not only raise an exception, it also stops the page from loading completely, therefore, no change was actually made in the for loop even though the buttons were clicked as every time the exception was raised, the load will be cancelled.