'WebDriver' object has no attribute 'switch_to_window'
时间: 2023-09-13 20:05:49 浏览: 252
This error message indicates that you are trying to use the 'switch_to_window' method on a WebDriver object, but this method does not exist in the WebDriver class.
The correct method to switch between windows in Selenium WebDriver is 'switch_to.window()'. Here is an example of how to use it:
```
# Get the current window handle
current_window = driver.current_window_handle
# Open a new window
driver.execute_script("window.open('https://www.google.com');")
# Switch to the new window
for window_handle in driver.window_handles:
if window_handle != current_window:
driver.switch_to.window(window_handle)
break
# Do something in the new window
print(driver.title)
# Switch back to the original window
driver.switch_to.window(current_window)
```
阅读全文