Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
学习Selenium,Python。页面对象有问题_Python_Selenium_Pageobjects - Fatal编程技术网

学习Selenium,Python。页面对象有问题

学习Selenium,Python。页面对象有问题,python,selenium,pageobjects,Python,Selenium,Pageobjects,我目前正在阅读selenium的文档。这部分让我有点困惑。专用于基本页的一部分。其描述如下: class BasePage(object): """Base class to initialize the base page that will be called from all pages""" def __init__(self, driver): self.driver = driver 但是

我目前正在阅读selenium的文档。这部分让我有点困惑。专用于基本页的一部分。其描述如下:

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

    def __init__(self, driver):
        self.driver = driver
但是如果这是suppost,那么它将成为所有类的父类。这个类是否应该负责实例化驱动程序并将驱动程序的方法包装成更合适的代码?我的意思是:

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" )

因此,现在只需访问frontpage并搜索项目就非常简单了。这是正确的想法还是我把它弄糊涂了?

将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创建两个不同的类。

将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创建两个不同的类