Dbao_miao 2021-10-25 17:18 采纳率: 60%
浏览 406
已结题

missing 1 required positional argument: 'self'。我这实例化有错误吗?



```python
from selenium.webdriver.chrome import webdriver
from selenium.webdriver.firefox import webdriver
from selenium import webdriver


class PageElements:
    """
    浏览器元素
    """

    def __init__(self):
        print("y")

    def chrome_ele(self):
        """
        谷歌
        :return:
        """
        chr_driver = webdriver.Chrome()
        self.user_name = chr_driver.find_element_by_id("kw")
        self.pdw = chr_driver.find_element_by_id('loginpwd')
        self.login_btn = chr_driver.find_element_by_id('loginBtn')

    def ie_ele(self):
        """
        ie
        :return:
        """
        ie_driver = webdriver.Ie()
        self.ie_usen = ie_driver.find_element_by_id("loginUserName")
        self.ie_usepwd = ie_driver.find_element_by_id("loginPwd")
        self.ielogin_btn = ie_driver.find_element_by_id("loginBtn")
        pass

    def firefox_ele(self):
        """
        firefox
        :return:
        """
        fire_driver = webdriver.Firefox()
        self.fire_usen = fire_driver.find_element_by_id("loginUserName")
        self.fire_usepwd = fire_driver.find_element_by_id("loginPwd")
        self.firelogin_btn = fire_driver.find_element_by_id("loginBtn")
        pass

    def edge_ele(self):
        """
        edge
        :return:
        """
        edeg_driver = webdriver.Edge()
        self.edge_usen = edeg_driver.find_element_by_id("loginUserName")
        self.edge_useped = edeg_driver.find_element_by_id("loginPwd")
        self.edgelogin_btn = edeg_driver.find_element_by_id("loginBtn")


chrome_ele = PageElements.chrome_ele()
ie_ele = PageElements.ie_ele()
fire_ele = PageElements.firefox_ele()
edge_ele = PageElements.edge_ele()








![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/281354351536190.png "=600 #left")
  • 写回答

1条回答 默认 最新

  • 辉煌仪奇 2021-10-25 17:21
    关注

    你好,python类是需要初始化的,你没有执行初始化就开始调用函数出错了,代码做如下修改即可

    
    from selenium.webdriver.chrome import webdriver
    from selenium.webdriver.firefox import webdriver
    from selenium import webdriver
    
    
    class PageElements:
        """
        浏览器元素
        """
    
        def __init__(self):
            print("y")
    
        def chrome_ele(self):
            """
            谷歌
            :return:
            """
            chr_driver = webdriver.Chrome()
            self.user_name = chr_driver.find_element_by_id("kw")
            self.pdw = chr_driver.find_element_by_id('loginpwd')
            self.login_btn = chr_driver.find_element_by_id('loginBtn')
    
        def ie_ele(self):
            """
            ie
            :return:
            """
            ie_driver = webdriver.Ie()
            self.ie_usen = ie_driver.find_element_by_id("loginUserName")
            self.ie_usepwd = ie_driver.find_element_by_id("loginPwd")
            self.ielogin_btn = ie_driver.find_element_by_id("loginBtn")
            pass
    
        def firefox_ele(self):
            """
            firefox
            :return:
            """
            fire_driver = webdriver.Firefox()
            self.fire_usen = fire_driver.find_element_by_id("loginUserName")
            self.fire_usepwd = fire_driver.find_element_by_id("loginPwd")
            self.firelogin_btn = fire_driver.find_element_by_id("loginBtn")
            pass
    
        def edge_ele(self):
            """
            edge
            :return:
            """
            edeg_driver = webdriver.Edge()
            self.edge_usen = edeg_driver.find_element_by_id("loginUserName")
            self.edge_useped = edeg_driver.find_element_by_id("loginPwd")
            self.edgelogin_btn = edeg_driver.find_element_by_id("loginBtn")
    
    PageElementsobjec=PageElements()
    chrome_ele = PageElementsobjec.chrome_ele()
    ie_ele = PageElementsobjec.ie_ele()
    fire_ele = PageElementsobjec.firefox_ele()
    edge_ele = PageElementsobjec.edge_ele()
    

    有帮助请采纳,有问题继续交流,你的采纳是对我回答的最大的肯定和动力

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月2日
  • 已采纳回答 10月25日
  • 创建了问题 10月25日

悬赏问题

  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 chatgpt网页版侧边栏和模型切换点击没反应怎么办
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas