Home > Enterprise >  How to create a custom Class - Selenium chromedriver
How to create a custom Class - Selenium chromedriver

Time:10-25

using pycharm, python 3.10, selenium 4.5 chrome Version 106.0.5249.119 and chromedriver 106.0.5249.61. If someone could assist me in structuring my class with multiple files please I would be very appreciative! I think I am not passing through the attributes in the class correctly or importing the packages correctly into it.

Im working from this simple/flat code which works perfect:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
   
s = Service("C:\\SeleniumDrivers\\chromedriver.exe")

driver = webdriver.Chrome(service=s)
driver.get("http://www.google.co.uk")

Now I am trying to create a Class in its own file:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

class Scrape(webdriver.Chrome(service=Service('C:\\SeleniumDrivers\\chromedriver.exe'))):
    def __init__(self):
        super(Scrape, self).__init__()

the run.py file is

from folder.scrape import Scrape

inst = Scrape()
inst.get("http://www.google.co.uk")
inst.maximisewindow ....etc
el1 = inst.findelement.......etc

the error message im getting File "C:\folder\scrape.py", line 12, in class Scrape(webdriver.Chrome(service=Service('C:\SeleniumDrivers\chromedriver.exe'))): File "C:\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in init super().init(DesiredCapabilities.CHROME['browserName'], "goog", File "C:\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 82, in init if options._ignore_local_proxy: AttributeError: 'dict' object has no attribute '_ignore_local_proxy'

CodePudding user response:

this action webdriver.Chrome(service=Service('C:\\SeleniumDrivers\\chromedriver.exe')) creates an object
so by this line class Scrape(webdriver.Chrome(service=Service('C:\\SeleniumDrivers\\chromedriver.exe'))) you actually inherit Scrape class not from webdriver.Chrome but from newly created object of that class
you'd better do it like this:

class Scrape(webdriver.Chrome):
    def __init__(self):
        super(Scrape, self).__init__(service=Service('C:\\SeleniumDrivers\\chromedriver.exe'))

here we call the constructor of webdriver.Chrome passing into it needed service argument

  • Related