【问题标题】:Learning Selenium, Python. Problem with Page object学习硒,Python。页面对象的问题
【发布时间】:2020-09-07 17:55:13
【问题描述】:

我目前正在阅读有关 selenium 的文档。这部分docs 让我有点困惑。 Perticually 是基本页面的一部分。描述如下:

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 BasePage(object):

    def __init__(self):
        self.driver = webdriver.Firefox()

    def findElementsByCss(self,selector):
        return self.driver.find_element_by_css_selector(selector)

    def visit(self,url):
        return self.driver.get(url)

那么其他页面不需要知道驱动就可以了

class FrontPage(BasePage):

    def searchForItem(self,item)
        return findElementsByCss(".input").send_keys( "hey" )

所以现在访问首页并搜索项目非常简单。这是正确的想法还是我混淆了它?

【问题讨论】:

    标签: python selenium pageobjects


    【解决方案1】:

    将 webdriver 作为类构造函数的参数传递使其更通用。 你可以:

    firefox_driver = webdriver.Firefox()
    firefox_browser = BasePage(firefox_driver)
    chrome_driver = webdriver.Chrome() (I hope that this method works)
    chrome_browser = BasePage(chrome_driver)
    

    您不需要为 Firefox 和 Chrome 创建两个不同的类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-30
      • 2022-01-17
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2018-10-22
      相关资源
      最近更新 更多