我正在尝试使用 Python selenium webdriver 切换到新打开的窗口。该代码之前运行良好,但现在显示错误。令人惊讶的是,Python 无法识别 switch_to_window() 方法,也没有要转到的声明。
def process_ebl_statements(self, account_number):
current_window = self.driver.current_window_handle
all_windows = self.driver.window_handles
print("Current window: ", current_window)
print("All windows: ", all_windows)
number_of_windows = len(all_windows)
self.driver.switch_to_window(all_windows[number_of_windows - 1])
错误详情:
'WebDriver' object has no attribute 'switch_to_window'
最佳答案
这个错误信息...
'WebDriver' object has no attribute 'switch_to_window'
...暗示WebDriver对象不再支持属性 switch_to_window()
切换到窗口
switch_to_window在 Selenium v2.41 中已弃用:
Selenium 2.41
- deprecating switch_to_* in favour of driver.switch_to.*
因此您会看到错误。
解决方案
您需要使用 switch_to 而不是 switch_to_window
.
例子:
driver.switch_to.active_element
driver.switch_to.alert
driver.switch_to.default_content()
driver.switch_to.frame()
driver.switch_to.parent_frame()
driver.switch_to.window('main')
关于python - 为什么 switch_to_window() 方法不适用于 Python 中的 selenium webdriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70360072/