'Code unreachable when adding Selenium ChromeOptions

For some reason my Python code displays as unreachable after adding a series of WebDriver options. Does anyone know why this is happening and how it can be fixed? enter image description here

Code itself is below:

class DriverOptions(object):

  def __init__(self):

    self.options = Options()
    self.options.add_argument('--no-sandbox')
    self.options.add_argument('--start-maximized')
    self.options.add_argument('--start-fullscreen')
    self.options.add_argument('--single-process')
    self.options.add_argument('--disable-dev-shm-usage')
    self.options.add_argument("--incognito")
    self.options.add_argument('--disable-blink-features=AutomationControlled')
    self.options.add_argument('--disable-blink-features=AutomationControlled')
    self.options.add_experimental_option('useAutomationExtension', False)
    self.options.add_experimental_option("excludeSwitches", ["enable-automation"])
    self.options.add_argument("disable-infobars")

    self.helperSpoofer = Spoofer()

    self.options.add_argument('user-agent={}'.format(self.helperSpoofer.userAgent))
    self.options.add_argument('--proxy-server=%s' % self.helperSpoofer.ip)


Solution 1:[1]

Possibly you are using too many arguments and you can remove some of the arguments which are no more relevant:

  • Remove the --no-sandbox argument and execute as non-root user.
  • Either use --start-maximized or --start-fullscreen, avoid using both.
  • If you aren't using --no-sandbox you can also remove --disable-dev-shm-usage'
  • Remove the --incognito argument as it is no more effective.
  • Remove the --disable-infobars argument as it is no more effective.
  • Add the argument '--disable-blink-features=AutomationControlled' only once.
  • The argument --single-process looks to me an overkill as it runs the renderer and plugins in the same process as the browser and you may like to drop it.

A simpler yet effective code block can be:

class DriverOptions(object):
    def __init__(self):
        self.options = Options()
        self.options.add_argument('--start-maximized')
        self.options.add_argument('--disable-blink-features=AutomationControlled')
        self.options.add_experimental_option('useAutomationExtension', False)
        self.options.add_experimental_option("excludeSwitches", ["enable-automation"])
        self.options.add_argument("disable-infobars")
        self.helperSpoofer = Spoofer()
        self.options.add_argument('user-agent={}'.format(self.helperSpoofer.userAgent))
        self.options.add_argument('--proxy-server=%s' % self.helperSpoofer.ip)

Solution 2:[2]

This bug is in selenium v4.1.3-4.1.4.

Update the Selenium library.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium
Solution 2 ?????? ??????????