Home > Back-end >  WebDriver object has no attribute 'switch_to' in Selenium Python
WebDriver object has no attribute 'switch_to' in Selenium Python

Time:07-04

Whenever I reach a JavaScript alert and try to interact with it, the test fails with an AttributeError, 'WebDriver' (in this case, 'JsAlerts') object has no attribute switch_to. I am using selenium version 4.3.0 and Python 3.10. Here is what I've tried so far, with no success:

The "browsers" fixture just yields a WebDriver instance for Chrome.

switch_to.alert:

from objects.javascript_alerts import JsAlerts
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def test_regular_alert(browsers):
    alerts = JsAlerts(browsers)
    alerts.load()
    alerts.click_go_to_js_alert()
    alerts.click_alert()
    WebDriverWait(alerts, 10).until(EC.alert_is_present())
    alerts.switch_to.alert.accept()

importing Alert

from objects.javascript_alerts import JsAlerts
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.alert import Alert

def test_regular_alert(browsers):
    alerts = JsAlerts(browsers)
    alerts.load()
    alerts.click_go_to_js_alert()
    alerts.click_alert()
    WebDriverWait(alerts, 10).until(EC.alert_is_present())
    al = Alert(alerts)
    al.accept()

"click_alert()" just means that I am clicking the button that makes the alert appear

I've also tried switch_to_alert()

CodePudding user response:

Alert should be part of driver.

Browser.switch_to.alert.accept()

CodePudding user response:

You can use

alert = browser.switch_to.alert

and then perform tasks like text, send_keys, accept, dismiss

  • Related