首页 > 解决方案 > 在父类python selenium中导入方法

问题描述

我有一个非常基本的问题,但我正在学习 Python。我想在使用 Selenium 的子类中使用从父级导入的模块。但是我在传递 child 中的参数时遇到问题。错误仍然是“模块”对象不可调用(最后一行)。我的可重现示例:

#my_file.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

from bs4 import BeautifulSoup
import urllib.request

class MyParent:
    def __init__(self, URL):
        self.URL = URL

        # Browser options
        self.options = Options()
        self.options.headless = False # Set True for Headless mode
        self.driver = webdriver.Chrome(options=self.options)
        self.delay = 3
        self.driver.get(self.URL)
        self.WebDriverWait = WebDriverWait(self.driver, self.delay)
        self.EC = EC() 

from my_file import MyParent as MyParent

class myChild(MyParent):
    def __init__(self):
        super().__init__("http://www.python.org")

    def my_function(self):
        elem = self.driver.find_element_by_name("q") #Here is working
        wait = self.WebDriverWait 
        wait.until(self.EC.element_to_be_clickable((By.ID, "id-search-field"))) #!!!!THE ERROR

c = myChild() 
c.my_function()


TypeError                                 Traceback (most recent call last)
<ipython-input-2-a9a7698ab26a> in <module>
----> 1 c = myChild()
      2 c.my_function()

<ipython-input-1-12db5db612ff> in __init__(self)
      3 class myChild(MyParent):
      4     def __init__(self):
----> 5         super().__init__("http://www.python.org")
      6 
      7     def my_function(self):

~/Desktop/0.3/my_file.py in __init__(self, URL)
     22         self.driver.get(self.URL)
     23         self.WebDriverWait = WebDriverWait(self.driver, self.delay)
---> 24         self.EC = EC()

TypeError: 'module' object is not callable

标签: pythonseleniumclassimportparent

解决方案


推荐阅读