WebDriver switchTo().alert() 函数失败

hak*_*uro 0 python selenium getelementbyid selenium-webdriver

我的 html 中有这一行:

<a class="btn" onclick="return confirm('Are you sure you want to delete this post?')" href="#">
Run Code Online (Sandbox Code Playgroud)

尝试切换到弹出窗口,然后使用以下命令单击“确定”

driver.switchTo().alert().accept()
Run Code Online (Sandbox Code Playgroud)

但它一直给我 AttributeError: 'WebDriver' 对象没有属性 'switchTo'。我还尝试找到弹出窗口按钮的元素 ID,但无法使其工作。任何建议将不胜感激。

And*_*son 10

Python你应该使用

driver.switch_to.alert.accept()
Run Code Online (Sandbox Code Playgroud)

switchTo()原样Java方法