ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.64k stars 1.14k forks source link

wait until element_to_be_clickable not working #1523

Open Lyfhael opened 1 year ago

Lyfhael commented 1 year ago

undetected-chromedriver version : 3.5.3 selenium version : 4.11.2 os: windows 11

Hello, I am trying to make a very basic script that access a url and click on the login button. I'm not being blocked, by using driver.save_screenshot('nowsecure2.png') I can see that the page is displayed and the login button visible.

However when the script reaches this line : login_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='//discord.com/login']")))

It throws an error :

Traceback (most recent call last):
  File "C:\Users\xxx\Documents\Programs\xxx\xxx\main.py", line 35, in <module>
    create_discord_account("random@email.com", "ahcujefe", "iojffjeeufj")
  File "C:\Users\xxx\Documents\Programs\xxx\xxx\main.py", line 21, in create_discord_account
    login_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='//discord.com/login']")))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Exception ignored in: <function Chrome.__del__ at 0x000001DEB946B740>
Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 843, in __del__
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 798, in quit
OSError: [WinError 6] Descripteur non valide

I tried without undetected_chromedriver and it works, but I want to use undetected_chromedriver.

Here is the full code :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import undetected_chromedriver as uc
import time

def create_discord_account(email, username, password):
    # create a new browser instance
    driver = uc.Chrome(headless=True, use_subprocess=False)

    # navigate to the Discord registration page
    driver.get("https://discord.com/")
    # driver = webdriver.Chrome()

    wait = WebDriverWait(driver, 20)
    login_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='//discord.com/login']")))
    login_button.click()
    driver.save_screenshot('nowsecure2.png')
    register_button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "smallRegisterLink-1qEJhz")))
    # click the register button
    register_button.click()
    driver.save_screenshot('nowsecure3.png')

    time.sleep(10)

    # close the browser
    driver.quit()

if __name__ == "__main__":
    create_discord_account("random@email.com", "ahcujefe", "iojffjeeufj")

Thank you

LUXTACO commented 1 year ago

Apparently a Timeout exception is triggered, try making the timeout a bit longer by modifying The second arg in the wait function, if that doesn't work try using XPATH instead of CSS_SELECTOR!

Also one more thing to make your coding way more efficient and clean, is to clean up the imports, remove double imports such as:

from selenium import webdriver

I would also recommend to have them in a sort of pyramid from short name imports to long name imports, or have them separated, just to improve workflow!

import time
from selenium import webdriver
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC