Home > OS >  How can I implement options in my oop python code?
How can I implement options in my oop python code?

Time:09-17

Help me, I don't know how can I implement options in my oop code, I don't want browser pop up every time I run the code, I have to add headless option but I don't know how

I do know how to make my code headless in functional or imperative paradigm, but I can't figure out how to implement this in oop paradigm, whatever I do, I approach to an error, I don't know what to do, I would be extremely thankful to who guild me to light my code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
os.environ["PATH"] = "C:\\SeleniumWebDrivers"

op = webdriver.ChromeOptions()
op.add_argument("--disable-blink-features=AutomationControlled")
op.add_argument("headless")

class Avamovie(webdriver.Opera(options=op)):
    def __init__(self, teardown=False):
        self.teardown = teardown
        super(Avamovie, self).__init__()
        self.implicitly_wait(15)
        self.maximize_window()
        self.minimize_window()
    def __exit__(self, exc_type, exc_val, exc_tb) -> None:
        if self.teardown:
            self.quit()

    def get_the_page(self):
        self.get("https://avamovie17.xyz")

    def search_box(self, title):
        sbox = self.find_element_by_css_selector("input[placeholder='جستجو']")
        sbox.send_keys(title)
        sbox.click()
        r = self.find_element_by_css_selector("ul[class='search_result_list']")
        r = r.find_element_by_tag_name("li").find_element_by_css_selector(
            "span[class='title']"
        )
        r.click()

    def download_links(self):
        links = self.find_elements_by_css_selector("a[href*='.mkv']")
        links = [link.get_attribute("href") for link in links]
        print(links)


with Avamovie() as scraper:
    scraper.get_the_page()
    scraper.search_box("A hero 2021")
    scraper.download_links()

my latest error:

Traceback (most recent call last):
  File "c:\Users\yasin\Desktop\pf\Projects\Selena\selena.py", line 1, in <module>
    from sites.avamovie import Avamovie
  File "c:\Users\yasin\Desktop\pf\Projects\Selena\sites\avamovie.py", line 10, in <module>
    class Avamovie(webdriver.Opera(options=op)):
  File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\opera\webdriver.py", line 79, in __init__
    OperaDriver.__init__(self, executable_path=executable_path,
  File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\opera\webdriver.py", line 55, in __init__
    ChromiumDriver.__init__(self,
  File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\common\service.py", line 71, in start
    cmd.extend(self.command_line_args())
  File "C:\Users\yasin\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\chrome\service.py", line 45, in command_line_args
    return ["--port=%d" % self.port]   self.service_args
TypeError: %d format: a real number is required, not dict

I would appreciate any help, thanks guys

CodePudding user response:

You appear to be trying to inherit from an instance of Opera, not the class itself. The options are passed to the call to super().__init__.

class Avamovie(webdriver.Opera):
    def __init__(self, options, teardown=False):
        self.teardown = teardown
        super(Avamovie, self).__init__(options)
        self.implicitly_wait(15)
        self.maximize_window()
        self.minimize_window()

    ...

with Avamovie(op) as scraper:
    scraper.get_the_page()
    scraper.search_box("A hero 2021")
    scraper.download_links()
  • Related