How to force webpage to stop loading to click a button?

How to force webpage to stop loading to click a button?

QA 0

Sometimes, when automating a webpage with Selenium, a script on the page or another reason prevents the webpage from loading in a timely fashion, preventing Selenium from accessing its elements such as buttons that we’d like to interact with.

In the code sample below, we solve this problem by replicating user interaction with the webpage via scrolling down a bit, and then we forcefully stop the page from continuing to load. This enables us to interact with webpage elements, like our button that we’d like to click, which are otherwise visible.

Python Code:

import win32com.client
from win32com.client import Dispatch
x = win32com.client.Dispatch("AppRobotic.API")
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://www.google.com')
# wait 20 seconds
x.Wait(20000)
# scroll down a couple of times in case page javascript is waiting for user interaction
x.Type("{DOWN}")
x.Wait(2000)
x.Type("{DOWN}")
x.Wait(2000)
# forcefully stop pageload at this point
driver.execute_script("window.stop();")
# if clicking with Selenium still does not work here, use screen coordinates
x.MoveCursor(xCoord, yCoord)
x.MouseLeftClick
x.Wait(2000)