【问题标题】:How to execute methods in a class using Selenium and Python如何使用 Selenium 和 Python 在类中执行方法
【发布时间】:2020-09-22 16:55:15
【问题描述】:

我最近找到了谷歌浏览器的 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'

有谁知道我做错了什么?

【问题讨论】:

  • run.setup_method("ac")
  • 你有什么不明白的?该错误准确地告诉您出了什么问题,您缺少必需的参数method。请注意,您对此不做任何事情,因此您似乎应该从定义中省略它

标签: python python-3.x selenium methods python-class


【解决方案1】:

此错误消息...

Missing 1 required positional argument: 'method'

...暗示setup_method() 缺少必需的位置参数,即“方法”


分析

你已经很接近了。根据setup_method(self, method) 的定义,它需要一个参数作为方法

def setup_method(self, method):

但是当你调用setup_method() 时:

run.setup_method()

你还没有传递任何参数。因此存在参数不匹配,您会看到错误。


解决方案

要在 Class 中使用 Selenium 执行您的测试,您可以使用以下解决方案:

  • 代码块:

    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
    
  • 浏览器快照:

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多