Edge configuration in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.edge.service import Service from selenium.webdriver.edge.options import Options from selenium.webdriver.common.by import By import time # Configure Edge options options = Options() options.add_argument("--start-maximized") options.add_argument("--disable-notifications") # Initialize Edge driver with options service = Service() # Assumes msedgedriver is in PATH try: driver = webdriver.Edge(service=service, options=options) driver.get("https://example.com") # Verify page title assert driver.title == "Example Domain", f"Expected title 'Example Domain' but got '{driver.title}'" finally: driver.quit()
This script starts by importing necessary Selenium classes for Edge browser automation.
We create an Options object to add arguments that start the browser maximized and disable notifications, matching the manual test case requirements.
The Service object is created without arguments assuming the Edge WebDriver executable is in the system PATH.
We then create the Edge driver with these options and navigate to 'https://example.com'.
An assertion checks that the page title exactly matches 'Example Domain'. If not, it raises an error with a clear message.
Finally, the driver.quit() call in the finally block ensures the browser closes even if the assertion fails or an error occurs.
Now add data-driven testing to open three different URLs with the same Edge configuration and verify their titles.
Practice
Options() in Edge configuration with Selenium?Solution
Step 1: Understand the role of
Options()Options()is used to configure browser settings such as running in headless mode or setting window size.Step 2: Differentiate from
Service()Service()is used to specify the driver location, not browser settings.Final Answer:
To set browser settings like headless mode or window size -> Option BQuick Check:
Options() sets browser settings = A [OK]
- Confusing Options() with Service()
- Thinking Options() locates elements
- Assuming Options() writes test assertions
Solution
Step 1: Use Options() to add headless argument
Create an Options object and add the '--headless' argument usingadd_argument().Step 2: Pass options to Edge driver constructor
Pass the options object towebdriver.Edge()using theoptionsparameter.Final Answer:
options = Options() options.add_argument('--headless') driver = webdriver.Edge(options=options) -> Option AQuick Check:
Options() + add_argument('--headless') + driver = Edge(options=options) = A [OK]
- Trying to add arguments to Service()
- Passing string directly to webdriver.Edge()
- Using wrong parameter names
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument('--window-size=800,600')
service = Service('path/to/msedgedriver')
driver = webdriver.Edge(service=service, options=options)
print(driver.execute_script('return window.innerWidth + "," + window.innerHeight'))
driver.quit()Solution
Step 1: Analyze the options argument
The code sets the window size to 800x600 using--window-size=800,600.Step 2: Understand the JavaScript execution
The script returns the current window inner width and height as a string separated by a comma.Final Answer:
"800,600" -> Option AQuick Check:
Window size set to 800x600, script returns "800,600" = C [OK]
- Assuming default window size is returned
- Confusing Service() with Options()
- Expecting syntax or runtime errors
from selenium import webdriver
from selenium.webdriver.edge.options import Options
options = Options()
options.add_argument('--headless')
driver = webdriver.Edge(options=options, executable_path='msedgedriver.exe')Solution
Step 1: Check driver initialization parameters
Theexecutable_pathparameter is deprecated in recent Selenium versions for Edge.Step 2: Use Service() to specify driver location
The correct way is to create aServiceobject with the driver path and pass it via theserviceparameter.Final Answer:
Using 'executable_path' parameter is deprecated; should use Service() -> Option CQuick Check:
Use Service() for driver path, not executable_path [OK]
- Using deprecated executable_path parameter
- Not importing Service class
- Thinking Options() disallows headless
Solution
Step 1: Add headless and user-agent arguments to Options()
Useadd_argument()to add both '--headless' and '--user-agent=MyCustomAgent' correctly.Step 2: Specify driver path using Service()
Create aServiceobject with the driver path and pass it towebdriver.Edge()via theserviceparameter.Step 3: Verify correct usage of parameters
from selenium import webdriver options = Options() options.headless = True options.user_agent = 'MyCustomAgent' driver = webdriver.Edge(executable_path='C:/drivers/msedgedriver.exe', options=options) incorrectly uses deprecatedexecutable_pathand wrong attribute names. from selenium import webdriver service = Service('C:/drivers/msedgedriver.exe') driver = webdriver.Edge(service=service) driver.add_argument('--headless') driver.add_argument('user-agent=MyCustomAgent') tries to add arguments to driver object which is invalid. from selenium import webdriver options = Options() options.add_argument('--headless') options.add_argument('--user-agent=MyCustomAgent') driver = webdriver.Edge(options=options) misses driver path specification.Final Answer:
from selenium import webdriver from selenium.webdriver.edge.service import Service from selenium.webdriver.edge.options import Options options = Options() options.add_argument('--headless') options.add_argument('--user-agent=MyCustomAgent') service = Service('C:/drivers/msedgedriver.exe') driver = webdriver.Edge(service=service, options=options) -> Option DQuick Check:
Options add_argument + Service for driver path = D [OK]
- Using deprecated executable_path instead of Service
- Adding arguments to driver instead of Options
- Missing driver path in configuration
Ready to prove it?
Pass the test and this pattern moves unknown
