kenJH
2021-08-24 10:17
采纳率: 100%
浏览 45
已结题

TypeError: open() missing 1 required positional argument: 'self'

from selenium import webdriver

class slorigen(object):
# 定义浏览器
  def __init__(self,brower):
      if brower == 'firefox' or brower =='Firefox' or brower == 'F' or brower == 'f':
          driver = webdriver.Firefox()
      elif brower == 'chrome' or brower == 'Chrome':
          driver = webdriver.Chrome()
      else :
          print("请使用火狐或谷歌浏览器")

 #打开网页
  def open(self, url):
    self.driver.get(url)  

引用该方法

from slconfig import slorigen

  if __name__ == '__main__':
    d = slorigen(brower='chrome')
    d = slorigen.open(url='http://www.baidu.com')

使用该方法时报错TypeError: open() missing 1 required positional argument: 'url'

  • 收藏

1条回答 默认 最新

  • 咕泡-三木 2021-08-24 14:21
    已采纳
    d = slorigen(brower='chrome')
    d.open(url='http://www.baidu.com')  # slorigen已经实例化为d了,所以调用d的open方法
    

    另外你的slorigen定义也有问题,没加self,driver就只是一个局部变量,用后即毁

    class slorigen(object):
    # 定义浏览器
      def __init__(self,brower):
          if brower == 'firefox' or brower =='Firefox' or brower == 'F' or brower == 'f':
              self.driver = webdriver.Firefox()                             ####
          elif brower == 'chrome' or brower == 'Chrome':
             self.driver = webdriver.Chrome()                             ####
          else : 
              print("请使用火狐或谷歌浏览器")
     
     #打开网页
      def open(self, url):
        self.driver.get(url)
    
    已采纳该答案
    1 打赏 评论

相关推荐 更多相似问题