pythonselenium-webdriverselenium-chromedriverpageloadpageloadstrategy

Selenium unable to stop page loading


I do not face this problem with other websites but on this specific website:

1- I want to get my internet speed while using speedtest website by Selenium but the page cant stop loading. Because of that, I think that reason that I can't get the website elements.

2- Moreover, any line after self.driver.get can't be excited only if I stop the code.

enter image description here

Code trials:

from selenium import webdriver
from selenium.webdriver.common.by import By

class InternetSpeedTwitterBot:
    def __init__(self):
        self.driver = webdriver.Chrome()

    def get_internet_speed(self):
        self.driver.get("https://www.speedtest.net/")
        print("go")
        self.driver.find_element(By.CLASS_NAME, "start-text").click()  # click on go 

browser = InternetSpeedTwitterBot()
browser.get_internet_speed()

I tried this method that I find it on more than one website but it did not help me.

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class InternetSpeedTwitterBot:
    def __init__(self):
        capa = DesiredCapabilities.CHROME
        capa["pageLoadStrategy"] = "none"
        self.driver = webdriver.Chrome(desired_capabilities=capa)
        self.wait = WebDriverWait(self.driver, 20)

    def get_internet_speed(self):
        self.driver.get("https://www.speedtest.net/")
        self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, "start-text")))
        self.driver.execute_script("window.stop();")
        print("go")
        self.driver.find_element(By.CLASS_NAME, "start-text").click()  # click on go

browser = InternetSpeedTwitterBot()
browser.get_internet_speed()

I don't understand anything but I just try it and it shows me an error:

    self.driver = webdriver.Chrome(desired_capabilities=capa)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: WebDriver.__init__() got an unexpected keyword argument 'desired_capabilities'

Solution

  • page_load_strategy

    page_load_strategy is now an attribute. So you need to configure page_load_strategy as follows:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.page_load_strategy = 'none'
    

    Solution

    With those minor few tweaks your working code will be:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    class InternetSpeedTwitterBot:
        def __init__(self):
            options = Options()
            options.add_argument("start-maximized")
            options.page_load_strategy = 'none'
            self.driver = webdriver.Chrome(options=options)
            self.wait = WebDriverWait(self.driver, 20)
    
        def get_internet_speed(self):
            self.driver.get("https://www.speedtest.net/")
            self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
            self.driver.execute_script("window.stop();")
            self.driver.find_element(By.CSS_SELECTOR, "span.start-text").click()
    
    browser = InternetSpeedTwitterBot()
    browser.get_internet_speed()