Forums

Package undetected_chromedriver not working

I want to use the undetected_chromedriver package, however it is not working.

I get a NoSuchElementException. However, when I run the same code locally on my device, works fine.

Here is the code:

import config
import time
import undetected_chromedriver as uc
import random
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

import telebot

chrome_options = uc.ChromeOptions()

chrome_options.add_argument("--disable-gpu")

bot = telebot.TeleBot(config.token)

driver = uc.Chrome(options = chrome_options,version_main = 90,headless=True,no_sandbox=True)

bot.send_message(config.chat_id_serc, 'started')

url = 'someurl'
time.sleep(2)
driver.get(url)
time.sleep(10)
driver.find_element('name', 'Username').send_keys(config.membership_no)

The only difference is that locally I can use chrome driver version 112 (version_main = 112).

You need to follow configuration from https://help.pythonanywhere.com/pages/selenium for selenium to work.

Thanks. I just noticed that I need to run it with headless=False. Therefore, I need a virtual display. However, pyvirtualdisplay does not work for me (RuntimeError: can't start new thread)

Any idea on how to make it work?

If you are trying that in a web app, you cannot create threads. See https://help.pythonanywhere.com/pages/AsyncInWebApps/ for how you can run that sort of code outside of your web app.