Skip to content
Advertisement

Shorten Selenium-Wire Option

I would like to improve the following case by shorting it. I’ve come this far

if decision == 'y':
    seleniumwire_options = {
        'proxy': {
            'http': f'https://usr:pw@page:port',
        }
    }

if decision == 'n':
    seleniumwire_options = {
        'proxy': {
            'no_proxy': 'localhost,127.0.0.1'
        }
    }

You could say that I should just remove the n decision, but I can’t because I need seleniumwire_options later.

webdriver.Chrome(..., seleniumwire_options = seleniumwire_options)

Advertisement

Answer

You can shorten it by factoring out common parts, like this:

if decision == 'y':
    proxy = {
        'http': f'https://usr:pw@page:port',
    }

if decision == 'n':
    proxy = {
        'no_proxy': 'localhost,127.0.0.1',
    }


seleniumwire_options = {
    'proxy': proxy
}

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement