Effective Browser Navigation Techniques Using Python and Selenium
Written on
In the previous article, we discussed various waiting strategies. This installment will focus on navigating through URLs, windows, frames, and alerts using Selenium.
Opening a Website
Launching a website is the primary action after initiating a web browser. In Selenium, this is accomplished by invoking the get method on the WebDriver instance, as shown below.
Implementation Detail
- The get method triggers the execute(Command.GET, {'url':url}) function. The provided URL is utilized in the POST request to the /session/:sessionId/url endpoint.
- The execute method calls the command executer’s execute method, which is an instance of the RemoteConnection class, responsible for communication with the server via the WebDriver wire protocol.
- The command's JSON response is parsed into a dictionary object.
- You can find the command mappings like Command.GET in the __init__ method of the RemoteConnection class.
Retrieving the Current URL
To obtain the current URL, utilize the current_url property of the WebDriver instance.
# output Current url: https://www.python.org/
Implementation Detail
This current_url call executes the following code:
return self.execute(Command.GET_CURRENT_URL)['value']
A GET request is made to the /session/:sessionId/url endpoint using the same mechanism as outlined in the get method.
Switching Between Windows, Tabs, and Iframes
WebDriver treats windows and tabs as equivalent. Each window possesses a unique identifier, known as a window handle, accessed via the current_window_handle property.
Switching to a Window
When clicking a link that opens a new window/tab, WebDriver may not automatically recognize the active window. It is necessary to switch to the new window or tab for interaction.
The following code snippet demonstrates opening a web page, clicking a link that opens in a new tab, and switching to it by comparing window handles. The window_handles property provides a list of window handles for the current session.
After switching, all WebDriver calls will direct to the new window.
Handling Frames and Iframes
Although frames are deprecated in HTML5, it is still possible to embed browsing contexts from different domains using the <iframe> tag.
Keep in mind that this code will not function because Selenium can only recognize elements in the top-level document. You must switch to the frame first to interact with its elements.
Switching to a Frame
There are three methods for switching to a frame:
- Using a WebElement: Identify the frame with your preferred selector and switch to it.
- By Name or ID: If the iframe has an id or name attribute, you can switch to it. If there are multiple matches, the first one found will be selected.
- Using an Index: You can also switch using the frame's index.
# switch to the second frame driver.switch_to.frame(1)
To exit an iframe or frameset, revert to the default content with the following command:
driver.switch_to.default_content()
Switching to Alerts
Selenium WebDriver provides built-in capabilities for managing popups. You can switch to an alert as follows:
alert = driver.switch_to.alert
This command switches to the currently open alert, allowing interaction such as accepting or dismissing the alert.
A future post will address handling popups in greater detail.
Closing a Window or Tab
Once you finish working with a window or tab, it is advisable to close it and return to the original window.
If you neglect to switch back and attempt to invoke a method on the driver object, a NoSuchWindowException will occur.
Exiting the Browser
To terminate the browser session, call the quit method on the WebDriver instance. The quit() method will:
- Close all associated windows and tabs.
- Terminate the browser process.
- End the background driver process.
If only one window is open, calling close() will have the same effect as quit(). Failing to call quit may leave background processes and ports active on your machine.
Using try/finally blocks can ensure the browser closes, or utilizing the driver instance as a context manager will automatically quit the driver at the end of execution.
Key Takeaways
- The first action after creating a WebDriver instance should be invoking the get method to access a website.
- You can simulate browser navigation with the back, forward, and refresh methods.
- Use switch_to.window and switch_to.frame to change focus between different windows and frames.
- The window_handles property returns all window handles for the current session, while current_window_handle provides the handle for the active window.
- Always remember to quit the browser to prevent resource leaks.
In the next article, I will explore keyboard controls and action chains. Thank you for reading!