Home > other >  Creating new python Class with Selenium webdriver
Creating new python Class with Selenium webdriver

Time:04-28

Good morning.

I'm new to Selenium and I have some issues with Selenium 4. Basically I'm trying to create a class that will have some custom methods like sending a request to specified URL. But...I'm stuck at init method. With the code I wrote the browser is starting but it breaks with error message:

====== WebDriver manager ====== Current google-chrome version is 101.0.4951 Get LATEST chromedriver version for 101.0.4951 google-chrome Driver [/home/agent2/.wdm/drivers/chromedriver/linux64/101.0.4951.41/chromedriver] found in cache Traceback (most recent call last): File "/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 71, in start self.process = subprocess.Popen(cmd, env=self.env, File "/usr/lib/python3.10/subprocess.py", line 966, in init self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/lib/python3.10/subprocess.py", line 1842, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/agent2/1-Python/Selenium-Scrapy/bot/booking/booking.py", line 29, in a = MyBot() File "/home/agent2/1-Python/Selenium-Scrapy/bot/booking/booking.py", line 17, in init super(MyBot, self).init() File "/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in init super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog",
File "/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 90, in init self.service.start() File "/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 81, in start raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

This is my code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


class MyBot(webdriver.Chrome):
    """My class"""

    def __init__(
        self,
        driver=webdriver.Chrome(
            service=Service(ChromeDriverManager().install()),
            options = webdriver.ChromeOptions()
            )
        ):
        self.driver = driver
        super(MyBot, self).__init__()
        
        
        
    
    def land_first_page(self):
        """My custom method"""

        self.get('My-url')



a = MyBot()
a.land_first_page()

To be honest I don't know how to solve this... Found some solutions but with Selenium 3 where you could simply pass a path to driver location. Thanks!

CodePudding user response:

This is because your class inherits from webdriver.Chrome, WebDriver __init__ tries to start a service using the chromedriver.exe file, but since you didn't provide a path it uses the default value executable_path="chromedriver" which doesn't exists in your project.

Just remove the inheritance and use the driver instance ChromeDriverManager().install() creates for you.

  • Related