WebDriver对象没有属性' switch_to'在硒中
每当我达到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题
该错误告诉您您的
JSALERTS
类没有正确的方法或属性,称为switch_to
是正确的。但是,您正在对待jsalerts
,就像是webdriver
对象一样。例如,您的行执行以下行不起作用,因为
警报
期望webdriver
对象,并且您正在传递自定义jsalerts
对象:对于您的
WebDriverWait
行也是如此,因此
在测试中,请使用
web driver
对象, do 具有switch_to
属性 寻找!我尝试使用 Pylenium ,这就是我想到的,所以您有一个工作示例。
和第二个测试随附的页面对象
Problem
The error is telling you that your
JsAlerts
class does not have a method or property calledswitch_to
which is correct. However, you're treatingJsAlerts
as if it is aWebDriver
object.For example, your line that does the following won't work because
Alert
expects aWebDriver
object and you're passing in a customJsAlerts
object:The same is true for your
WebDriverWait
line:Solutions
So, in your test, use the
WebDriver
object which does have theswitch_to
property you're looking for!I tried the same exercise using Pylenium and this is what I came up with so you have a working example.
And the page object that goes along with the second test
警报应成为驱动程序的一部分。
Alert should be part of driver.
您可以使用
,然后执行
text
,send_keys
,接受
, divive> dimption 之类的任务执行任务。You can use
and then perform tasks like
text
,send_keys
,accept
,dismiss