When we use selenium to simulate login, after obtaining the login button element, we can directly call its click() method to achieve login jump, and the webDriver at this time also points to
This is fine for the current page, but it should be noted that because the page loading speed is generally lower than the program running speed, before obtaining the elements of the page after login, you can use WebDriverWait's util method, or you can directly use Thread.sleep() to let the program sleep for a while (not recommended).
But the main point of the blog is that if we enter the new page by clicking on the normal hyperlink, then we cannot get the new page element through the above method, because the webDriver at this time still points to the previous page.
Solution: First, get all handles of the window, and then iterate through this handle collection until the handle exists is different from the current window's handle (currentHandle), and transfer the previous webDriver to the page pointed to by the new handle.
The code is as follows:
String currentWindow=driver.getWindowHandle(); //Get the handle of the current window Set<String> handles=driver.getWindowHandles(); //Get the handles of all windows Iterator<String> it=handles.iterator(); WebDriver newDriver=null; while (it.hasNext()){ String handle=it.next(); if(!handle.equals(currentWindow)){ driver=driver.switchTo().window(handle); //Switch to the window break pointed to by the new handle; } }The solution to the failure of obtaining new page elements based on selenium is the entire content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.