how to procses Cloudflare Turnstile in web driver with selenuim - python

i try to use selenuim module to get for some website unsuccsesfull.
this is what i try:
undetected_chromedriver
add user profile
use proxy.
when i open it with webdriver the Cloudflare not allow me to process.
when i open it with normal chrom its works fine.
any idias?
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from datetime import datetime
import time
from undetected_chromedriver import Chrome
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print('Hey Elior, im on = ', current_time)
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 12")
driver = Chrome(chrome_options=options)
driver.get("https://www.example.com")
def login():
driver.get("site")
# identify username, password and signin elements
time.sleep(1)
driver.find_element(By.NAME, "username").click()
driver.find_element(By.NAME, "username").send_keys('')
time.sleep(1)
driver.find_element(By.NAME, "password").click()
driver.find_element(By.NAME, "password").send_keys('')
time.sleep(1)
# press on login button
driver.find_element(By.ID, "login-submit").click()
driver.maximize_window()
driver.execute_script("console.clear()") # clean the console
time.sleep(3)

You could try using Selenium-Profiles.
Note that headless almost never works undetected.

Related

Can't register on site using webdriver

For training purposes trying to test registering using Selenium WebDriver. Manually could do this and navigating to profile page, but using WebDriver after submitting username/email and password I stay on the same page not logging. Though registration is perfromed successfully as I could log in using specified credentials.
Manual scenario:
Navigate to https://hdrezka.ag/
Click register button to open register pop-up
register_button = driver.find_element(By.CSS_SELECTOR, 'a.b-tophead__register')
Fill in email, login, password fields.
Click register_submit_button:
register_submit_button = driver.find_element(By.XPATH, "//button[#name='submit']")
If you do it manually element profile_dropdown will be displayed. If do it using script (I set breakpoint to try clicking register_submit_button manually as well), element profile_dropdown will not be displayed.
profile_dropdown = wait.until(EC.visibility_of_element_located ((By.XPATH, "//span[#class='b-tophead-dropdown'] [text()='Профиль']")))
I tried:
incognito mode
disable-blink-features
disabling extensions
setting user agent
Maybe you have some other ideas?
from selenium.webdriver import Chrome, Firefox
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
path = r'' # TODO: specify path to webdriver
options = Options()
options.add_argument("--incognito")
options.add_argument("--disable-blink-features")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--disable-extensions")
ua = UserAgent()
userAgent = ua.random
print(userAgent)
options.add_argument(f'--user-agent={userAgent}')
driver = Chrome(service=Service(path), chrome_options=options)
driver.maximize_window()
driver.get('https://hdrezka.ag/')
agent = driver.execute_script('return navigator.userAgent;')
print(agent)
register_button = driver.find_element(By.CSS_SELECTOR, 'a.b-tophead__register')
register_button.click()
wait = WebDriverWait(driver, 5)
register_popup = wait.until(EC.visibility_of_element_located((By.ID, 'register-popup')))
time.sleep(5)
from random import randint
valid_register_login = f"user{randint(1, 100000000000)}"
valid_register_email = f"{valid_register_login}#test.com"
valid_register_password = ''.join([chr(randint(48, 57)) + chr(randint(65, 90)) + chr(randint(97, 122)) for _ in range(5)])
print()
print(f"login: {valid_register_login}")
print(f"email: {valid_register_email}")
print(f"password: {valid_register_password}")
input_register_email = driver.find_element(By.ID, 'email')
input_register_email.send_keys(valid_register_email)
email_validity_checker = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '#result-registration-email span.string-ok')))
assert email_validity_checker.is_displayed()
input_register_login = driver.find_element(By.ID, 'name')
input_register_login.send_keys(valid_register_login)
login_validity_checker = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '#result-registration-login span.string-ok')))
assert login_validity_checker.is_displayed()
input_register_password = driver.find_element(By.ID, 'password1')
input_register_password.send_keys(valid_register_password)
register_submit_button = driver.find_element(By.XPATH, "//button[#name='submit']")
register_submit_button.click()
# Failed here as page is not updated when using script
profile_dropdown = wait.until(EC.visibility_of_element_located ((By.XPATH, "//span[#class='b-tophead-dropdown'] [text()='Профиль']")))
assert profile_dropdown.is_displayed()
driver.quit()
Expected element after successful registration:

Can't login gmail in the background Python + Selenium

options = webdriver.ChromeOptions()
options.add_argument("--log-level=3")
options.add_argument("--headless")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-web-security')
options.add_argument('--user-data-dir')
options.add_argument('--allow-running-insecure-content')
options.add_argument("--window-size=1920,1080")
options.add_argument('--disable-logging')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-blink-features=AutomationControlled')
user_agent = 'My user agent'
options.add_argument(f'user-agent={user_agent}')
driver = uc.Chrome(options=options, version_main=109)
driver.get('https://gmail.com')
driver.find_element("xpath", '//*[#id="identifierId"]').send_keys(gmail)
driver.get_screenshot_as_file('other.png')
time.sleep(1)
driver.find_element("xpath", '//*[#id="identifierNext"]/div/button').click()
time.sleep(5)
driver.get_screenshot_as_file('onasini.png')
driver.find_element("xpath", '//*[#id="password"]/div[1]/div/div[1]/input').send_keys(gmail_password)
time.sleep(2)
driver.find_element("xpath", '//*[#id="passwordNext"]/div/button').click()
I tried everything but nothing worked, I needed to log in to Gmail using the "headless" argument
or in the background and it was showing this error "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id= "password"]/div[1]/div/div[1]/input"}". I worked with undetected_chromedriver
in my understanding, using sendkeys() and click() is not working on Goolge. because they detect bots by that. so another way is use pyautogui.
It does work, but while script is running, you cannot use user mouse or keyboard. because pyautogui are using. and also cannot use headless option
import pyautogui
import time
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import undetected_chromedriver as uc
user_id = "YOUR ID"
user_pw = "YOUR PASSWORD"
options = uc.ChromeOptions()
# options.add_argument('--headless')
driver = uc.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.implicitly_wait(15)
driver.get(url='https://accounts.google.com/signin/v2/identifier?hl=ko&passive=true&continue=https%3A%2F%2Fwww.google.com%2F'
'%3Fgws_rd%3Dssl&ec=GAZAmgQ&flowName=GlifWebSignIn&flowEntry=ServiceLogin')
time.sleep(5)
pyautogui.write(user_id)
pyautogui.press('tab', presses=3)
pyautogui.press('enter')
time.sleep(10)
pyautogui.write(user_pw)
pyautogui.press('enter')
this is work on
python==3.9
selenium==4.1.5
undetected-chromedriver==3.4.6
PyAutoGUI==0.9.53

Automating Facebook using Selenium Webdriver

driver = webdriver.Chrome('chromedriver')
driver.get('https://www.facebook.com/')
print("opened facebook")
I am using this code to open Facebook and the page opens.
driver.find_element(By.NAME, "email").send_keys("xxx")
sleep(1)
driver.find_element(By.NAME, "pass").send_keys("xxx")
sleep(1)
driver.find_element(By.NAME, "login").click()
sleep(1)
Then log in to my account. After successful login, my chrome window closes in a few seconds.
Can someone tell me why?
Full Code:
import time
import os
import wget
import shutil
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
try:
usr=""
pwd=""
driver = webdriver.Chrome('chromedriver')
driver.get('https://www.facebook.com/')
print ("Opened facebook")
driver.find_element(By.NAME, "email").send_keys(usr)
print ("Email Id entered")
sleep(1)
driver.find_element(By.NAME, "pass").send_keys(pwd)
print ("Password entered")
driver.find_element(By.NAME,"login").click()
sleep(100)
except Exception as e:
print("The error raised is: ", e)
The program will exit after executing code. Add below statements to keep program running:
time.sleep(300) #300 seconds i.e. 5 minutes
# close the browser window
driver.quit()
This will fix your problem
from selenium.webdriver.chrome.options import Options
# Stop Selenium from closing browser automatically
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
# Chrome driver to run chrome
driver = webdriver.Chrome(options=chrome_options)

Python using selenium webdriver to fill a form, when submitting the form, chrome is saying no internet connection is available

I am using Selenium webdriver to fill out a form, the fields are good, but when I do a .submit using the login button, or do a .submit after the password field, the chrome browser will say "Unable to connect to internet. Please check your internet connection."
the internet is definitely up since I am able to open other websites on another browser (including a new session of chrome).
This is my code:
import time
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = webdriver.ChromeOptions();
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation']);
options = Options()
# options.add_argument('start-maximumized')
# options.add_argument('disable-infobars')
chrome_options.add_argument('no-sandbox')
PATH = "c:\scripts\chromedriver.exe"
# driver = webdriver.Chrome(executable_path=PATH)
driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path='c:\scripts\chromedriver.exe')
driver.get('https://stupidwebsite.org')
time.sleep(1)
input_username = driver.find_element_by_id('username')
time.sleep(1)
input_username.send_keys("myself#email.com")
input_password = driver.find_element_by_id('password')
input_password.send_keys("mypassword#123")
input_password.submit()
# esubmit=driver.find_element_by_xpath
('/html/body/div/div[1]/div[2]/div/div/form/div[2]/div/button')
# esubmit = driver.find_element_by_name('MuiButton-label')
# esubmit.submit()
time.sleep(2)
# driver.quit()
I changed it a bit, and tried out google, and it works:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\scripts\chromedriver.exe")
driver.implicitly_wait(0.5)
#launch URL
driver.get("https://www.google.com/")
#identify search box
m = driver.find_element_by_name("q")
#enter search text
m.send_keys("Tutorialspoint")
time.sleep(0.2)
#perform Google search with Keys.ENTER
m.send_keys(Keys.ENTER)
Please help me out. Thank you.

Selenium closing the brower alone after finishing all the function

I'm pretty new in programming so I might be an easy question, but I don't understand why the browsers opened by Selenium closes at the end of the code.
from lib2to3.pgen2 import driver
from selenium import webdriver
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
You can use 2 approaches in order to keep you driver open.
1.
Add the 'detach' option to your driver settings:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
Simply add a delay at the end of your test code (less elegant approach but more simple)
from lib2to3.pgen2 import driver
from selenium import webdriver
import time
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
time.sleep(50)
This is because after the all functions, The code stops running and that's why selenium exits.
You can use the time module to delay.
import time
from lib2to3.pgen2 import driver
from selenium import webdriver
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
time.sleep(50) #---> 50 second delay

Categories