pythongoogle-chromeweb-scrapingselenium-chromedriverundetected-chromedriver

Webscraping with undetected_chromedriver, ubuntu/windows 11 compatibility issue


i coded discord bot in python, that is basically webscraping chat gpt 3.5 and sending answers to my discord server. It works on windows 11, but no matter what i do, i cant run it on my ubuntu server. Here is code that is outputting error (everything to line 25):

from bs4 import BeautifulSoup
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By # target


import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!", intents=discord.Intents.all())



import time



# files
#import login



options = uc.ChromeOptions() 
options.headless = False


chrome = uc.Chrome(options=options) 

as i said it works on windows but when i run it on my ubuntu server (using this command: xvfb-run --server-args="-screen 0 1920x1080x24" python3 AiSupport.py, i need xvfb so i can run it in headful mode, undetected chrome driver doesent really work in headless mode ) it gives this error:

ubuntu@instance-20230620-2312:~/bot$ xvfb-run --server-args="-screen 0 1920x1080                                                                                                                                                                                                x24" python3 AiSupport.py
Traceback (most recent call last):
  File "/home/ubuntu/bot/AiSupport.py", line 25, in <module>
    chrome = uc.Chrome(options=options)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/undetected_chromedriver                                                                                                                                                                                                /__init__.py", line 466, in __init__
    super(Chrome, self).__init__(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/chro                                                                                                                                                                                                me/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/chro                                                                                                                                                                                                mium/webdriver.py", line 53, in __init__
    self.service.start()
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/comm                                                                                                                                                                                                on/service.py", line 98, in start
    self._start_process(self._path)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/comm                                                                                                                                                                                                on/service.py", line 204, in _start_process
    self.process = subprocess.Popen(
  File "/usr/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.10/subprocess.py", line 1863, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/home/ubuntu/.local/share/undetected_chro                                                                                                                                                                                                medriver/undetected_chromedriver'

i tried updating python and modules on my ubuntu server, didnt work


Solution

  • You can use SeleniumBase's UC Mode as an alternative, which has a slightly modified version of undetected-chromedriver.

    First pip install seleniumbase. Then run the following script with python:

    from seleniumbase import SB
    
    with SB(uc=True) as sb:
        sb.driver.get("https://nowsecure.nl/#relax")
        sb.sleep(3)
    

    It automatically configures settings based on the environment. For example, it knows when to use Xvfb in a headless Linux environment.