python - 为什么 switch_to_window() 方法不适用于 Python 中的 selenium webdriver?

标签 python selenium selenium-webdriver webdriver webautomation

我正在尝试使用 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'

enter image description here

最佳答案

这个错误信息...

'WebDriver' object has no attribute 'switch_to_window'

...暗示WebDriver对象不再支持属性 switch_to_window()


切换到窗口

switch_to_windowSelenium 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/

相关文章:

Python元组问题

javascript - 如何等到元素的文本在 WebdriverJS 中发生变化?

java - 使用 Selenium WebDriver 将鼠标悬停并单击隐藏元素

java - 如何在显示文本为动态的网页上定位元素

python - RNN 的 Tensorflow 示例

python - Gtk.Grid() : how to dynamically modify the number of rows

selenium.windowfocus() 这个命令有什么用?

java - 如何将xpath位置转换为像素

java - getAttribute 或 getText

python - 在 Python 上提取完整的字符串