python - 创建驱动基类并继承webdriver

标签 python python-3.x selenium selenium-webdriver selenium-chromedriver

在Python/Selenium中,我主要尝试创建一个浏览器类,这样我就可以创建自定义函数,例如click或type等,并通过键入简单的内容(例如browser.click(element))轻松调用这些函数browser.type_text(element, text)

我认为我走在正确的轨道上,但我无法让继承发挥作用。例如,当我使用 browser = Browser() 创建浏览器实例并尝试使用 Webdriver 的正常功能(如 browser.get(webpage))时,我收到错误说明没有 GET 功能。我走在正确的轨道上吗?有更好的办法吗?

class Browser(webdriver.Chrome):
    def __init__():
        super(self).init()
        self.browser = webdriver.Chrome()

    def click(element):
        WebDriverWait(self, 10).until(EC.element_to_be_clickable(element)).click()

browser = Browser()
element = (By.ID, elements['remember'])
browser.click(element)

更新: 所以看起来我能够弄清楚我最初打算做什么。

我想使用一个类创建一个网络驱动程序,并基本上扩展该库以包含一些自定义函数。我会解释一下。

class Browser(webdriver.Chrome):
    def __init__(self):
        super().__init__()

    def click(self, element):
        WebDriverWait(self, 10).until(EC.element_to_be_clickable(element)).click()

    def type(self, element, text):
        for i in text:
            WebDriverWait(browser, 10).until(EC.visibility_of_element_located(element)).send_keys(i)
            time.sleep(random.uniform(0.05,0.25))

所以基本上在这里我只是添加了一个自定义单击和键入功能,以便让网络驱动程序等待元素并单击或键入等更方便。

browser = Browser()
browser.click(element)
browser.type(element, text)

最佳答案

正如 @Guy 所提到的,您混淆了 WebDriver 类和 WebElement 类的功能。 WebDriver 类确实具有用于 URL 导航的 get 等方法,但 WebElement 类具有 click 等方法,并且很快。我不使用您当前的方法,而是推荐一种在 Selenium 中流行的设计模式:Page Object Model design pattern

遵循此设计模式将允许您轻松调用更简单的方法名称。例如,直接取自链接页面:

from element import BasePageElement
from locators import MainPageLocators

class SearchTextElement(BasePageElement):
    """This class gets the search text from the specified locator"""

    #The locator for search box where search string is entered
    locator = 'q'


class BasePage(object):
    """Base class to initialize the base page that will be called from all pages"""

    def __init__(self, driver):
        self.driver = driver


class MainPage(BasePage):
    """Home page action methods come here. I.e. Python.org"""

    #Declares a variable that will contain the retrieved text
    search_text_element = SearchTextElement()

    def is_title_matches(self):
        """Verifies that the hardcoded text "Python" appears in page title"""
        return "Python" in self.driver.title

    def click_go_button(self):
        """Triggers the search"""
        element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
        element.click()


class SearchResultsPage(BasePage):
    """Search results page action methods come here"""

    def is_results_found(self):
        # Probably should search for this text in the specific page
        # element, but as for now it works fine
        return "No results found." not in self.driver.page_source

这将允许您进行简单命名的方法调用:

MainPage.click_go_button()

关于python - 创建驱动基类并继承webdriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550027/

相关文章:

python - 如何将列添加到 Tkinter TreeView 小部件?

python - [ :] work in python? 如何

python - 来自分层代码列表的递归字典

python - 无法加载 dynlib/dll (Pyinstaller)

python - 基于颜色python的对象边界框

java - 访问样式属性中供应商特定的 CSS 属性? Selenium 似乎默认忽略它

java - 无法使用 Selenium Java 中的 sendKeys() 将文件上传到 "browse"按钮

python - 在 MQTT Python 中搜索字符串

python - azure 函数绑定(bind)日期时间昨天

php - 如何使用 phpunit-selenium 设置元素属性