The method of re-getting the handle after Python jumps to the page

Python + selenium operates on web pages, but after clicking on the page and jumping to the next page, the element cannot be found. After repeated inspection, it is found that the problem is not the element, but the window handle still stays on the previous page.

After discovering this problem, I searched Baidu for a long time and found that their method of re-acquiring the handle is not positioned using CSS elements. This is embarrassing. Had to change it a bit.

The specific code is as follows:

driver.switch_to_window(driver.window_handles[1])

PS: The way to get the window handle again in this section is to get the handle of the newly opened window, which is the handle of the second window in the browser. If you want to get the handle of the third window, please change driver.window_handles[1] to driver.window_handles[2], the order of obtaining window handles starts from 0, and the 1 in driver.window_handles[1] is the handle of the second window.

Let's test it, first open Baidu, then search the Python official website, click in, and a new Python official website page will be opened after clicking in, and click PSF on the Python official website. If you jump to the Python official website without re-acquiring the handle of the Python official website, you cannot click the PSF button.

Original code:

Add the code to relocate the handle to the code after the jump page to verify it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324941083&siteId=291194637