如何使用 Selenium 和 Python 在 class 中执行方法
How to execute methods in a class using Selenium and Python
我最近找到了 Google Chrome 的 SeleniumIDE 扩展,但有些地方我不明白...
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestStealth():
def setup_method(self, method):
print("setup_method")
self.driver = webdriver.Chrome()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_stealth(self):
print("Testing")
self.driver.get("https://stealthxio.myshopify.com/products/stealthxio-practice")
self.driver.set_window_size(968, 1039)
这是我从 selenium 获得的代码,当我尝试 运行 时:
run = TestStealth()
run.setup_method()
run.test_stealth()
我在 run.setup_method() 中收到一个错误:
Missing 1 required positional argument: 'method'
有谁知道我做错了什么?
这个错误信息...
Missing 1 required positional argument: 'method'
...暗示 setup_method()
缺少必需的位置参数,即 'method'
分析
你们非常接近。根据 setup_method(self, method)
的定义,它需要一个参数作为 method.
def setup_method(self, method):
但是当您调用 setup_method()
时:
run.setup_method()
您还没有通过任何参数。因此存在参数不匹配,您会看到错误。
解决方案
要使用 Class 使用 执行测试,您可以使用以下解决方案:
代码块:
class TestStealth():
def setup_method(self):
print("setup_method")
self.driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
self.vars = {}
def teardown_method(self):
self.driver.quit()
def test_stealth(self):
print("Testing")
self.driver.get("https://stealthxio.myshopify.com/products/stealthxio-practice")
self.driver.set_window_size(968, 1039)
run = TestStealth()
run.setup_method()
run.test_stealth()
控制台输出:
setup_method
DevTools listening on ws://127.0.0.1:51558/devtools/browser/88bf2c58-10da-4b03-9697-eec415197e66
Testing
浏览器快照:
我最近找到了 Google Chrome 的 SeleniumIDE 扩展,但有些地方我不明白...
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestStealth():
def setup_method(self, method):
print("setup_method")
self.driver = webdriver.Chrome()
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_stealth(self):
print("Testing")
self.driver.get("https://stealthxio.myshopify.com/products/stealthxio-practice")
self.driver.set_window_size(968, 1039)
这是我从 selenium 获得的代码,当我尝试 运行 时:
run = TestStealth()
run.setup_method()
run.test_stealth()
我在 run.setup_method() 中收到一个错误:
Missing 1 required positional argument: 'method'
有谁知道我做错了什么?
这个错误信息...
Missing 1 required positional argument: 'method'
...暗示 setup_method()
缺少必需的位置参数,即 'method'
分析
你们非常接近。根据 setup_method(self, method)
的定义,它需要一个参数作为 method.
def setup_method(self, method):
但是当您调用 setup_method()
时:
run.setup_method()
您还没有通过任何参数。因此存在参数不匹配,您会看到错误。
解决方案
要使用 Class 使用
代码块:
class TestStealth(): def setup_method(self): print("setup_method") self.driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe') self.vars = {} def teardown_method(self): self.driver.quit() def test_stealth(self): print("Testing") self.driver.get("https://stealthxio.myshopify.com/products/stealthxio-practice") self.driver.set_window_size(968, 1039) run = TestStealth() run.setup_method() run.test_stealth()
控制台输出:
setup_method DevTools listening on ws://127.0.0.1:51558/devtools/browser/88bf2c58-10da-4b03-9697-eec415197e66 Testing
浏览器快照: