If the link opens a new tab, your driver will still use the current window by default,
Alternatively, you can pass a “window handle” to the “switch_to_window()” method. Knowing this, it’s possible to iterate over every open window like so:
for handle in driver.window_handles:
driver.switch_to_window(handle)
For example, if your browser has several tabs, then window_handles saves the instance objects corresponding to these tabs, so if you only have one web page open currently, then the newly opened page is window_handles[1 ] After switching to that page, get the source code.
If it is opened in the current window, it is possible that the new page has not been loaded yet and the url and data of the new page cannot be obtained by then. You can use wait here and set some conditions to ensure that the new page is loaded before proceeding. Code As follows:
If the link opens a new tab, your driver will still use the current window by default,
For example, if your browser has several tabs, then window_handles saves the instance objects corresponding to these tabs, so if you only have one web page open currently, then the newly opened page is window_handles[1 ]
After switching to that page, get the source code.
If it is opened in the current window, it is possible that the new page has not been loaded yet and the url and data of the new page cannot be obtained by then. You can use wait here and set some conditions to ensure that the new page is loaded before proceeding. Code As follows: