WebDriver对象没有属性' switch_to'在硒中

发布于 2025-02-12 19:56:19 字数 2795 浏览 3 评论 0原文

每当我达到JavaScript警报并尝试与之交互时,测试都会使用属性“ WebDriver”(在这种情况下,'jsalerts')对象失败。我正在使用硒版4.3.0和Python 3.10。这是我到目前为止尝试的方法,但没有成功:

“浏览器”固定装置仅产生Chrome的WebDriver实例。

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()

导入警报

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()

我还尝试了switch_to_alert()

编辑:在页面对象和固定装置的一部分以获取更多上下文。

页面对象

class JsAlerts:
    # URL

    URL = 'https://the-internet.herokuapp.com/'

    # Locators

    add_js_alert_link = (By.LINK_TEXT, 'JavaScript Alerts')
    js_alert = (By.CSS_SELECTOR, "li:nth-child(1) > button")
    js_confirm = (By.CSS_SELECTOR, "li:nth-child(2) > button")
    js_prompt = (By.CSS_SELECTOR, "li:nth-child(3) > button")
    results = (By.ID, "result")

    # Initializer

    def __init__(self, browser):
        self.browser = browser

    # Interaction Methods

    def load(self):
        self.browser.get(self.URL)

    def click_go_to_js_alert(self):
        self.browser.find_element(*self.add_js_alert_link).click()

    def click_alert(self):
        self.browser.find_element(*self.js_alert).click()

fixture (在配置中设置chrome)

@pytest.fixture
def browsers(config):
    # Initialize the ChromeDriver instance
    if config['browser'] == 'Firefox':
        b = selenium.webdriver.Firefox()
    elif config['browser'] == 'Chrome':
        b = selenium.webdriver.Chrome()
    elif config['browser'] == 'Headless Chrome':
        opts = selenium.webdriver.ChromeOptions()
        opts.add_argument('headless')
        b = selenium.webdriver.Chrome(options=opts)
    else:
        raise Exception(f'Browser "{config["browser"]}" is not supported')

    # Return the WebDriver instance for the setup
    yield b

    # Quit the Webdriver instance for the cleanup
    b.quit()

使用浏览器.switch_to.alert.accept()仍然返回“'jsalerts的对象没有属性switch_to”错误。

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()

I've also tried switch_to_alert()

Edit: Posting parts of the page object and fixture for more context.

Page object

class JsAlerts:
    # URL

    URL = 'https://the-internet.herokuapp.com/'

    # Locators

    add_js_alert_link = (By.LINK_TEXT, 'JavaScript Alerts')
    js_alert = (By.CSS_SELECTOR, "li:nth-child(1) > button")
    js_confirm = (By.CSS_SELECTOR, "li:nth-child(2) > button")
    js_prompt = (By.CSS_SELECTOR, "li:nth-child(3) > button")
    results = (By.ID, "result")

    # Initializer

    def __init__(self, browser):
        self.browser = browser

    # Interaction Methods

    def load(self):
        self.browser.get(self.URL)

    def click_go_to_js_alert(self):
        self.browser.find_element(*self.add_js_alert_link).click()

    def click_alert(self):
        self.browser.find_element(*self.js_alert).click()

Fixture (Chrome is set up in config)

@pytest.fixture
def browsers(config):
    # Initialize the ChromeDriver instance
    if config['browser'] == 'Firefox':
        b = selenium.webdriver.Firefox()
    elif config['browser'] == 'Chrome':
        b = selenium.webdriver.Chrome()
    elif config['browser'] == 'Headless Chrome':
        opts = selenium.webdriver.ChromeOptions()
        opts.add_argument('headless')
        b = selenium.webdriver.Chrome(options=opts)
    else:
        raise Exception(f'Browser "{config["browser"]}" is not supported')

    # Return the WebDriver instance for the setup
    yield b

    # Quit the Webdriver instance for the cleanup
    b.quit()

Using browsers.switch_to.alert.accept() still returns the "'JsAlerts' object has no attribute switch_to " error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一影成城 2025-02-19 19:56:19

问题

该错误告诉您您的JSALERTS类没有正确的方法或属性,称为switch_to是正确的。但是,您正在对待jsalerts,就像是webdriver对象一样。

例如,您的行执行以下行不起作用,因为警报期望webdriver对象,并且您正在传递自定义jsalerts对象:

# change this:
al = Alert(alerts)

# to this:
al = Alert(browsers)

对于您的WebDriverWait行也是如此,

# change this:
WebDriverWait(alerts, 10)

# to this:
WebDriverWait(browsers, 10)

因此

在测试中,请使用web driver对象, do 具有switch_to属性 寻找!

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

我尝试使用 Pylenium ,这就是我想到的,所以您有一个工作示例。

# test_alers.py

from selenium.webdriver.common.alert import Alert
from pylenium.driver import Pylenium

from tests.ui.page import Page


def test_accept_alert(py: Pylenium):
    py.visit("https://the-internet.herokuapp.com/javascript_alerts")
    py.get("[onclick='jsAlert()']").click()
    alert: Alert = py.webdriver.switch_to.alert
    alert.accept()
    assert py.contains("You successfully clicked an alert")


def test_accept_alert_with_page_object(py: Pylenium):
    page = Page(py).goto()
    page.open_alert().accept()
    assert py.contains("You successfully clicked an alert")

和第二个测试随附的页面对象

# page.py

from selenium.webdriver.common.alert import Alert
from pylenium.driver import Pylenium


class Page:
    def __init__(self, py: Pylenium):
        self.py = py
        self.driver = py.webdriver

    def goto(self) -> "Page":
        self.py.visit("https://the-internet.herokuapp.com/javascript_alerts")
        return self

    def open_alert(self) -> Alert:
        self.py.get("[onclick='jsAlert()']").click()
        return self.driver.switch_to.alert

Problem

The error is telling you that your JsAlerts class does not have a method or property called switch_to which is correct. However, you're treating JsAlerts as if it is a WebDriver object.

For example, your line that does the following won't work because Alert expects a WebDriver object and you're passing in a custom JsAlerts object:

# change this:
al = Alert(alerts)

# to this:
al = Alert(browsers)

The same is true for your WebDriverWait line:

# change this:
WebDriverWait(alerts, 10)

# to this:
WebDriverWait(browsers, 10)

Solutions

So, in your test, use the WebDriver object which does have the switch_to property you're looking for!

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

I tried the same exercise using Pylenium and this is what I came up with so you have a working example.

# test_alers.py

from selenium.webdriver.common.alert import Alert
from pylenium.driver import Pylenium

from tests.ui.page import Page


def test_accept_alert(py: Pylenium):
    py.visit("https://the-internet.herokuapp.com/javascript_alerts")
    py.get("[onclick='jsAlert()']").click()
    alert: Alert = py.webdriver.switch_to.alert
    alert.accept()
    assert py.contains("You successfully clicked an alert")


def test_accept_alert_with_page_object(py: Pylenium):
    page = Page(py).goto()
    page.open_alert().accept()
    assert py.contains("You successfully clicked an alert")

And the page object that goes along with the second test

# page.py

from selenium.webdriver.common.alert import Alert
from pylenium.driver import Pylenium


class Page:
    def __init__(self, py: Pylenium):
        self.py = py
        self.driver = py.webdriver

    def goto(self) -> "Page":
        self.py.visit("https://the-internet.herokuapp.com/javascript_alerts")
        return self

    def open_alert(self) -> Alert:
        self.py.get("[onclick='jsAlert()']").click()
        return self.driver.switch_to.alert
み青杉依旧 2025-02-19 19:56:19

警报应成为驱动程序的一部分。

Browser.switch_to.alert.accept()

Alert should be part of driver.

Browser.switch_to.alert.accept()
拥有 2025-02-19 19:56:19

您可以使用

alert = browser.switch_to.alert

,然后执行textsend_keys接受, divive> dimption 之类的任务执行任务。

You can use

alert = browser.switch_to.alert

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文