Headless Browser using Google Chrome

You will learn how to execute your scripts in Headless browser using Google Chrome.
Chrome Options for running Chrome browser in headless mode can be accomplished by using the predefined arguments –headless.

Scenario :
         Open URL: http://newtours.demoaut.com
         Enter UserName and Password
         Click on the Login button.
         Verify the Window Title
         Close the browser

        Script:
      from selenium import webdriver
      from selenium.webdriver.chrome.options import Options
      chromePath="C:\\browserdrivers\\chromedriver.exe"
      chrome_opt=Options()
      chrome_opt.add_argument("--headless")
      driver=webdriver.Chrome(chromePath,options=chrome_opt)
      driver.get("http://newtours.demoaut.com")
      driver.find_element_by_name("userName").send_keys("hello")
      driver.find_element_by_name("password").send_keys("hello")
      driver.find_element_by_name("login").click()
      print(driver.title)
      driver.close()

      Code Explanation:
      Initially, you need to set the path to the chromedriver.exe file since you are using Chrome Browser for testing.

Next, create an object of Chrome Options class and pass it to web driver instance. Since we want to open Chrome browser in headless mode, we need to pass the argument –headless to Chrome Options class.

Create an object of Chrome Driver class and pass the Chrome Options object as an argument.

Finally, we need to pass the URL - http://demo.guru99.com/ to the driver.get method.

Print the page title and close the browser.

Comments