10 examples of 'how to open chrome browser in selenium python' in Python

Every line of 'how to open chrome browser in selenium python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
23def startBrowser(chrome_driver_path):
24 """Starts browser with predefined parameters"""
25 chrome_options = Options()
26 #chrome_options.add_argument('--headless')
27 driver = webdriver.Chrome(executable_path=chrome_driver_path, options=chrome_options)
28 return driver
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

10def getChromeBrowser(path: str = 'chromedriver', proxy: str = None,
11 implicitWaitTime: int = 30, incognito: bool = False,
12 headless: bool = False) -> webdriver.Chrome:
13 """
14 Returns an instance of the webdriver for Chrome.
15
16 :param path: The path to your chromedriver\n
17 :param proxy: Proxy to connect to - '<host>:<port>\n
18 :param implicitWaitTime: Implicit wait time for the browser\n
19 :param incognito: Whether to open in incognito or not\n
20 :param headless: Run the browser in headless mode
21 """
22 chromeOptions = webdriver.ChromeOptions()
23 chromeOptions.add_argument('disable-infobars')
24
25 if proxy:
26 chromeOptions.add_argument(f'--proxy-server={proxy}')
27 if incognito:
28 chromeOptions.add_argument("--incognito")
29 if headless:
30 chromeOptions.add_argument('--headless')
31
32 driver = webdriver.Chrome(
33 chrome_options=chromeOptions, executable_path=path)
34 driver.implicitly_wait(implicitWaitTime)
35 return driver
7def chrome_driver(executable_path=None, run_headless=False,
8 load_images=True, use_proxy=None):
9 '''Function to initialize ``selenium.webdriver.Chrome`` with extended options
10
11 Args:
12 executable_path (str): path to the chromedriver binary. If set to ``None`` selenium will serach for ``chromedriver`` in ``$PATH``.
13 run_headless (bool): boolean flag that indicates if chromedriver has to be headless (without GUI).
14 load_images (bool): boolean flag that indicates if Chrome has to render images.
15 use_proxy (str): use http proxy in <host:port> format.
16
17 Returns:
18 selenium.webdriver.Chrome: created driver.
19
20 Note:
21 In order to create Chrome driver Selenium requires `Chrome <https://www.google.com/chrome/browser/desktop/index.html>`_ to be installed and `chromedriver <https://sites.google.com/a/chromium.org/chromedriver/>`_ to be downloaded.
22
23 Warning:
24 Headless Chrome is shipping in Chrome 59 and in Chrome 60 for Windows. Update your Chrome browser if you want to use ``headless`` option.
25 '''
26 chrome_options = webdriver.ChromeOptions()
27 if run_headless:
28 chrome_options.add_argument('headless')
29 if not load_images:
30 prefs = {'profile.managed_default_content_settings.images': 2}
31 chrome_options.add_experimental_option('prefs', prefs)
32 if use_proxy:
33 chrome_options.add_argument('proxy-server=' + use_proxy)
34 if executable_path:
35 driver = webdriver.Chrome(chrome_options=chrome_options,
36 executable_path=executable_path)
37 else:
38 driver = webdriver.Chrome(chrome_options=chrome_options)
39 return driver
24def getDriver(browser):
25 chrome_options = webdriver.ChromeOptions()
26 chrome_options.add_argument("--incognito")
27 chrome_options.add_argument("--window-size=1440,900")
28 if browser.lower() == 'firefox':
29 driver = webdriver.Firefox()
30 elif browser.lower() == 'chrome':
31 driver = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
32 elif browser.lower() == 'chrome_linux':
33 driver = webdriver.Chrome('./chromedriver_linux64', chrome_options=chrome_options)
34 elif browser.lower() in ('phantomjs', 'headless'):
35 driver = webdriver.PhantomJS()
36 else:
37 print("WARNING: browser selection not valid, use PhantomJS as default")
38 driver = webdriver.PhantomJS()
39 return driver
10def getDriver():
11 driver = webdriver.Chrome()
12 return driver
51def open(self, driver_choice = Driver.PHANTOM):
52 try:
53 self._set_driver(driver_choice)
54 date = datetime.now().strftime("%y-%m-%d_%H-%M-%S")
55 self.log = Log(date)
56
57 self.log.append("PROGRAM STARTED!", False)
58 driver_name = next(name for name, value in vars(Driver).items() if value == driver_choice)
59 self.log.append("Driver : " + str(driver_name), False)
60
61 self._status_driver = True
62 return True
63 except Exception as exp:
64 self.log.append_exception(exp)
65 self._status_driver = False
66 return False
156def chromedriver():
157 package_name = "io.appium.android.apis"
158 package_name = "com.xueqiu.android"
159
160 with driver(package_name) as dr:
161 print(dr.current_url)
162 elem = dr.find_element_by_xpath('//*[@id="phone-number"]')
163 elem.click()
164 elem.send_keys("123456")
31@pytest.fixture(scope='session')
32def browser(live_server, driver):
33 '''Open a Selenium browser with window size and wait time set.'''
34
35 def go(self, url):
36 self._last_url = url
37 return self.get(self.live_server.url + url)
38
39 def dump(self, filename=None):
40 dest = filename or self._last_url.replace('/', '_').replace('#', '~')
41 self.get_screenshot_as_file('./{}.jpg'.format(dest))
42
43 b = driver
44 b.live_server = live_server
45 b.set_window_size(1024, 768)
46 b.implicitly_wait(10)
47
48 b.go = types.MethodType(go, b)
49 b.dump = types.MethodType(dump, b)
50
51 return b
34def open_browser(self):
35 # if self.hide_browser:
36 size = (self.MOBILE_WIDTH + 1, self.MOBILE_HEIGTH + 1)
37 self.display = Display(visible=int(not self.hide_browser), size=size)
38 self.display.start()
39 self.browser = self._open_mobile_browser()
19def __init__(self, kill_old=True, implicitly_wait=20):
20 if kill_old:
21 self.kill()
22 path = ChromeDriverManager().install()
23 Log.info('Starting Google Chrome ...')
24 profile_path = os.path.join(Settings.TEST_OUT_TEMP, 'chrome_profile')
25 Folder.clean(profile_path)
26 options = webdriver.ChromeOptions()
27 options.add_argument('user-data-dir={0}'.format(profile_path))
28 self.driver = webdriver.Chrome(executable_path=path, chrome_options=options)
29 self.implicitly_wait = implicitly_wait
30 self.driver.implicitly_wait(self.implicitly_wait)
31 self.driver.maximize_window()
32 self.focus()
33 Log.info('Google Chrome started!')

Related snippets