This context is about navigating among URLs, windows, frames, and alerts using Selenium in Python for browser automation.
Abstract
The context discusses various methods for navigating among URLs, windows, frames, and alerts using Selenium in Python for browser automation. It explains the use of the get method to open a website, the current_url property to query the current URL, and the back method to emulate the back button on the browser. The context also explains how to switch between windows/tabs/iframes using the switch_to.window and switch_to.frame methods, as well as how to switch to an alert using the switch_to.alert method. It emphasizes the importance of quitting the browser after use to avoid resource leaks.
Bullet points
Opening a website is done using the get method on a WebDriver instance.
The current URL can be queried using the current_url property of the WebDriver instance.
The back method is used to emulate the back button on the browser.
The switch_to.window and switch_to.frame methods are used to switch between windows/tabs/iframes.
The window_handles property returns a list of all window handles for the current session, and the current_window_handle property gives the handle of the window which the WebDriver instance is working on.
The switch_to.alert method is used to switch to an alert.
It is important to quit the browser after use to avoid resource leaks.
Browser Automation with Python and Selenium — 6: Navigation
In the previous post, we looked at different wait strategies. I will try to explain navigation among URLs, windows, frames, and alerts in this post.
Opening a Website
Opening a website is the first thing after starting a web browser. In Selenium this is done with the getmethod call on WebDriver instance like below.
Implementation Detail
getmethod invokes the execute(Command.GET, {'url':url}) call. Url passed is used in the POST request to /session/:sessionId/urlendpoint.
execute method calls the command executer’s execute method. Command executer is an instance of the RemoteConnection class. This RemoteConnection instance is the part that communicates with the server using the WebDriver wire protocol.
The command’s JSON response is loaded into a dictionary object.
You can find the command mappings like Command.GET in the __init__ method of the RemoteConnection class.
A GET request is done to /session/:sessionId/url endpoint with the same mechanism explained in the get method.
Going Back in the History
Calling the back method on the WebDriver instance is equivalent to pressing the browser’s back button.
The following example first opens a web page, clicks a link on the page, and then calls the back method to return to the home page again.
There are also forward and refresh methods used in the same way.
Switching Between Windows/Tabs/Iframes
WebDriver does not make the distinction between windows and tabs. Each window has a unique identifier that remains persistent in a single session. This is called a window handle and is accessed via current_window_handleproperty.
Switching to Window
If clicking a link opens a new window/tab, WebDriver will not know which window is considered active. We need to switch to the new window or tab to work with it.
The following code opens a web page, clicks a link that will be opened in a new tab, waits for it to open, and finally switches to it by comparing window handles. window_handlesproperty returns a list of window handles for the current session.
After switching to a new window, all calls to the driver will be directed to this window.
Frames and Iframes
Frames are deprecated in HTML5, but you can still insert a browsing context from another domain with the <iframe>tag.
Consider the following iframe and python snippet
This code will not work, because Selenium only knows elements in the top level document. We need to switch to the frame first to be able to click to button inside it.
You can find the frame using your preferred selector and switch to it.
Using a Name or ID
If your iframe has an id or name attribute, you can use this to switch to it. If the name or id is not unique on the page, then the first one found will be switched to.
Using an Index
You can also use the index of the frame to switch.
# switch to the second framedriver.switch_to.frame(1)
To leave an iframe or frameset, switch back to the default content as follows
driver.switch_to.default_content()
Switch to Alert
Selenium WebDriver has built-in support for handling popups. You can switch to an alert as follows
alert= driver.switch_to.alert
This will switch to the currently open alert and return the alert object that you can interact with the alert such as accepting or dismissing it.
I will write a separate post about managing popups later in this series.
Closing a Window/Tab
When you are finished with a window or tab, you should close it and switch back to the original window.
If you forget to switch window and try to call a method on the driver object, you will get a NoSuchWindowException.
Quitting the Browser
You can quit the browser by calling the quit method on the WebDriver instance.
quit() will
Close all the windows and tabs associated with that WebDriver session
Close the browser process
Close the background driver process
If you have only one window, close() will make the same effect as quit(). Forgetting to call quit will leave background processes and ports running on your machine.
You can use try/finally blocks to make sure that the browser is quit, or if you use the driver instance as a context manager, it will automatically quit the driver at the end of execution.
Things to Remember
One of the first things after creating a WebDriver instance is to call the getmethod to open a website.
You can emulate back, forward, refresh buttons on the browser with back, forward, and refresh methods.
switch_to.window and switch_to.frame is used to switch control among different windows/frames.
window_handles returns a list of all window handles for the current session. current_window_handle property gives the handle of the window which the WebDriver instance working on.
Don’t forget to quit the browser to avoid resource leaks.
In the next post, I will write about keyboard control and action chains. Thank you for your time.