undetected-chromedriver icon indicating copy to clipboard operation
undetected-chromedriver copied to clipboard

Chrome's Webdriver getting stuck on driver.get(URL)

Open mominurr opened this issue 1 year ago • 8 comments

    options = uc.ChromeOptions()
    options.add_argument("--excludeSwitches=enable-automation")
    options.add_argument("--excludeSwitches=enable-logging")
    prefs = {
    'credentials_enable_service': False,
    'profile': {
        'password_manager_enabled': False
    }
    }
    options.add_experimental_option('prefs', prefs)
    options.set_capability('unhandledPromptBehavior', 'dismiss')
    options.add_argument('--disable-extensions')
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-notifications')
    options.add_argument('--disable-popup-blocking')
    driver = uc.Chrome(options=options)
    driver.maximize_window()
    driver.set_page_load_timeout(80)
    # Set the script timeout to 20 seconds
    driver.set_script_timeout(30)

this my code that I am using. But Script running after some time chrome driver is stuck . New iteration can not excecuted (stuck on driver.get(URL). I am facing this problem recently. Please give me solution.

mominurr avatar Dec 25 '23 09:12 mominurr

What is the page you are trying to load? Because when used the driver you generated to load any page it did not get stuck.

But if this is happening with you I suggest that you to disable the page load strategy and create a custom one. I normally use this one:

from time import time, sleep
from undetected_chromedriver.options import ChromeOptions
from undetected_chromedriver import Chrome

def _get_custom_page_load_strategy(driver: Chrome, url: str) -> None:
    driver.get(url)

    start_time = time()
    while time() - start_time < 30:
        if driver.execute_script(
            "return document.readyState == 'complete' || document.readyState == 'interactive'"
        ):
            return

        sleep(1)

    raise TimeoutError()

chrome_options = ChromeOptions()
chrome_options.page_load_strategy = 'none' # disable page load strategy

driver = Chrome(options = chrome_options)

_get_custom_page_load_strategy(driver, 'https://nowsecure.nl')

pedro-peixot0 avatar Dec 26 '23 13:12 pedro-peixot0

https://aftermarket.schaeffler.com/ I am trying to load this website page. I am trying to scraping this website then I am facing this problem.

mominurr avatar Dec 26 '23 14:12 mominurr

It seems that the code I sent you solved the issue. The new problem is that the page is loaded too fast (before some components are completely loaded, but to solve this you could add a WebDriverWait. Something like this:

from time import time, sleep
from undetected_chromedriver.options import ChromeOptions
from undetected_chromedriver import Chrome

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def _get_custom_page_load_strategy(driver: Chrome, url: str) -> None:
    driver.get(url)

    start_time = time()
    while time() - start_time < 30:
        if driver.execute_script(
            "return document.readyState == 'complete' || document.readyState == 'interactive'"
        ):
            return

        sleep(1)

    raise TimeoutError()

chrome_options = ChromeOptions()
chrome_options.page_load_strategy = 'none' # disable page load strategy

driver = Chrome(options = chrome_options)

_get_custom_page_load_strategy(driver, 'https://aftermarket.schaeffler.com/')

wait = WebDriverWait(driver, 20)

wait.until(
    EC.element_to_be_clickable((By.CLASS_NAME, 'banner-actions-container'))
)

pedro-peixot0 avatar Dec 26 '23 15:12 pedro-peixot0

您尝试加载的页面是什么?因为当使用您生成的驱动程序加载任何页面时,它不会卡住。

但是,如果您遇到这种情况,我建议您禁用页面加载策略并创建自定义策略。我通常使用这个:

from time import time, sleep
from undetected_chromedriver.options import ChromeOptions
from undetected_chromedriver import Chrome

def _get_custom_page_load_strategy(driver: Chrome, url: str) -> None:
    driver.get(url)

    start_time = time()
    while time() - start_time < 30:
        if driver.execute_script(
            "return document.readyState == 'complete' || document.readyState == 'interactive'"
        ):
            return

        sleep(1)

    raise TimeoutError()

chrome_options = ChromeOptions()
chrome_options.page_load_strategy = 'none' # disable page load strategy

driver = Chrome(options = chrome_options)

_get_custom_page_load_strategy(driver, 'https://nowsecure.nl')

I ran this code, but it did not bypass cloudflare. Can you give me the code to bypass cloudflare? Thanks

wwangyu2 avatar Jan 30 '24 19:01 wwangyu2

Here is dirty trick that I am using to bypass cloud flare @wwangyu2. Basically the browser cannot be focused on the tab trying to bypass cloudflare.

from time import time, sleep
from undetected_chromedriver.options import ChromeOptions
from undetected_chromedriver import Chrome

def _get_with_cf_bypass(driver: Chrome ,url: str) -> None:
    # open a new tab and navigate to the url
    driver.execute_script(f"window.open('{url}', '_blank');")
    # waiting CloudFlare to redirect to the real page
    sleep(5)
    # switch to the new tab
    driver.switch_to.window(driver.window_handles[1]) 

def get_custom_page_load_strategy(driver: Chrome, url: str) -> None:
    _get_with_cf_bypass(driver ,url)

    start_time = time()
    while time() - start_time < 30:
        if driver.execute_script(
            "return document.readyState == 'complete' || document.readyState == 'interactive'"
        ):
            return

        sleep(1)

    raise TimeoutError()

chrome_options = ChromeOptions()
chrome_options.page_load_strategy = 'none' # disable page load strategy

driver = Chrome(options = chrome_options)

get_custom_page_load_strategy(driver, 'https://nowsecure.nl')

pedro-peixot0 avatar Jan 30 '24 21:01 pedro-peixot0