0

As title, I want to practice encapsulation of programing, and I have a problem about the code I write.

The browser always automatically opens when I call the class(CrawlerSetting), and I guess which code causes the situation (browser = wb.Chrome(options=options)), but I have no idea how to avoid this situation.

Here is my code:

from selenium import webdriver as wb

class CrawlerSetting:
    '''
    Description:
        chrome 84 version
    '''
    options = wb.ChromeOptions()    
    options.add_experimental_option('excludeSwitches', ["enable-automation"])  
    global browser 
    browser = wb.Chrome(options=options)   

    def __init__(self, target):
        self.target = target

    def open(self):
        return browser.get(self.target)

    def click_xpath(self, x_path):
        submit_element = browser.find_element_by_xpath(x_path)
        return submit_element.click()

It's my first time to ask question, if I have any format error . Plz tell me!

y88022
  • 1
  • 2
  • How's this related to encapsulation? – Vishesh Mangla Aug 09 '20 at 14:55
  • Maybe I misunderstand the encapsulation. Could you tell me the correct statement about my question? I'm not native speaker, Sorry! – y88022 Aug 09 '20 at 15:01
  • 1
    chrome_options.add_argument("--headless") was the command if I remember correctly for not opening up the browser – Vishesh Mangla Aug 09 '20 at 16:36
  • @y88022 Does this [discussion](https://stackoverflow.com/questions/46920243/how-to-configure-chromedriver-to-initiate-chrome-browser-in-headless-mode-throug/49582462#49582462) help you? – undetected Selenium Aug 09 '20 at 17:22
  • And ya, python doesn't have encapsulation, it has name mangling. To say oops are really easy to understand like one might be told in a text book but understanding their actual meaning and paradigm requires some digging up in the history. – Vishesh Mangla Aug 09 '20 at 20:59
  • Guys thx u very much! I'm sorry to have failed to make myself clear. Actually, I want to execute this class(CrawlerSetting) without running the chromedriver only I call it from another func. I wonder know which position is right according to my idea. – y88022 Aug 10 '20 at 01:05

0 Answers0