Search code examples
pythonseleniumselenium-chromedriverbrowser-automation

Selenium detection problem in Python. Any suggestions or solutions?


I used a selenium library for making my own Nike SNKRS Bot in python, which will work in Chrome browser. I chose one of popular webdriver to manage it by selenium. I got stuck on a Nike login page.

Here is my code:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType
from random_user_agent.user_agent import UserAgent
from selenium.common.exceptions import TimeoutException, WebDriverException
from random_user_agent.params import SoftwareName, OperatingSystem 
import traceback
options = Options()
options.add_argument("--start-maximized")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--lang=en')
options.add_experimental_option('useAutomationExtension', False)
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
options.add_argument('user-agent={0}'.format(user_agent))
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1420,1080")
service=Service('C:\\chromedriver_win32\\chromedriver.exe')
driver = webdriver.Chrome(options=options,service=service)
driver.delete_all_cookies()
driver.execute_script('return navigator.webdriver')
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"})
driver.get('https://www.nike.com')

def send_keys_delay(controller,keys,delay=1):
    controller.click()
    for key in keys:
        controller.send_keys(key)
        time.sleep(delay)
    controller.send_keys(Keys.RETURN)
try:
    elem=WebDriverWait(driver,5).until(
        ec.presence_of_element_located((By.ID,"hf_cookie_text_cookieAccept"))
    )
    print(elem.text)
    elem.click()
except:
    traceback.print_exc()
    driver.quit()
time.sleep(3)
try:
    elem=driver.find_element(By.ID,"hf_title_signin_membership")
    elem.click()
except Exception:
    traceback.print_exc()
    driver.quit()
try:
    elem=WebDriverWait(driver,5).until(
        ec.element_to_be_clickable((By.ID,"username")))
    email="myemail@gmail.com"
    send_keys_delay(elem,email)


except Exception:
    traceback.print_exc()
    driver.quit()

After all, an error message appears on a screen. This error refers to communication with server. Error message is:

Error parsing response from server.

I'm sure that Nike detects my program as a bot and I can't go further.

My question is: Is it possible to make it work using selenium? Or maybe I should try something different?


Solution

  • in regards to other platforms you can use, you can try puppeteer, its very similar to selenium and they have a stealth plugin to avoid detection as well as a 2captcha plugin in which you link 2captcha account to solve captchas to further avoid detection.