10

I am trying to access a site using selenium Python. But the site is checking and checking continuously by cloudflare. No other page is coming.

Check the screenshot here.

enter image description here

I have tried undetected chrome but it is not working at all.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
JamesHorab
  • 107
  • 1
  • 1
  • 7

3 Answers3

8

By undetected chrome do you mean undetected chromedriver?:

Anyways, undetected-chromedriver works for me:

Undetected chromedriver

Github: https://github.com/ultrafunkamsterdam/undetected-chromedriver

pip install undetected-chromedriver

Code that gets a cloudflare protected site:

import undetected_chromedriver as uc
driver = uc.Chrome(use_subprocess=True)
driver.get('https://nowsecure.nl')

My POV

enter image description here enter image description here


Quick setup code that logs into your google account:

Github: https://github.com/xtekky/google-login-bypass

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

#  ---------- EDIT ----------
email = 'email\n' # replace email
password = 'password\n' # replace password
#  ---------- EDIT ----------

driver = uc.Chrome(use_subprocess=True)
wait = WebDriverWait(driver, 20)
url = 'https://accounts.google.com/ServiceLogin?service=accountsettings&continue=https://myaccount.google.com%3Futm_source%3Daccount-marketing-page%26utm_medium%3Dgo-to-account-button'
driver.get(url)


wait.until(EC.visibility_of_element_located((By.NAME, 'identifier'))).send_keys(email)
wait.until(EC.visibility_of_element_located((By.NAME, 'password'))).send_keys(password)
print("You're in!! enjoy")

# [ ---------- paste your code here ---------- ]
xtekky
  • 572
  • 1
  • 4
  • 13
  • Hi, Thanks, but like I said undetected-chromedriver is not working on me. I have checked again. – JamesHorab Mar 17 '22 at 20:31
  • Check: https://stackoverflow.com/questions/56528631/is-there-a-version-of-selenium-webdriver-that-is-not-detectable you may find an answer there or https://blog.m157q.tw/posts/2020/09/11/bypass-cloudflare-detection-while-using-selenium-with-chromedriver/ – xtekky Mar 17 '22 at 20:39
  • 1
    @xtekky [undetected_chromedriver v1](https://stackoverflow.com/a/65534593/7429447) as of today appears vulnerable and we are still working to integrate [_`undetected_chromedriver`_](https://stackoverflow.com/a/66200459/7429447) with Selenium v4.x – undetected Selenium Mar 17 '22 at 21:46
  • Thank you! it worked perfectly for me. By adding the uc.Chrome(use_subprocess=True) it start working fine. Also I imported from selenium.webdriver.common.by import By and to find the elements by XPath I used:userTxtBox = self.driver.find_element(By.XPATH,'//*[@id="ctl00_ContentPlaceHolder_UserNameTextBox"]') – mavi Aug 08 '22 at 06:10
3

https://github.com/seleniumbase/SeleniumBase has undetected-chromedriver mode (--uc / uc=True).

After pip install seleniumbase, a script like this will bypass a site with Cloudflare turnstiles.

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.open("https://nowsecure.nl/#relax")
    sb.sleep(2)
    if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
        sb.get_new_driver(uc=True)
        sb.open("https://nowsecure.nl/#relax")
        sb.sleep(2)
    sb.assert_text("OH YEAH, you passed!", "h1", timeout=4)
    sb.sleep(2)

(Use sb.driver to access the raw driver.)

There's also a simple driver format:

from seleniumbase import Driver
import time

driver = Driver(uc=True)
driver.get("https://nowsecure.nl/#relax")
time.sleep(4)
driver.quit()

It can bypass detection on most sites:

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
1

The simple method is useful:

from selenium import webdriver
import time 

browser = webdriver.Chrome()
browser.get(url)

# sleep to wait pass
time.sleep(3) 

html_source = browser.page_source
print(html_soup)
smallyu
  • 31
  • 2