Does Webdriver support pagefactory for Python?
Asked Answered
V

4

7

I was reading about page objects and design patterns on the Webdriver project site and came across pagefactory. It doesn't look like the Webdriver for Python API includes pagefactory. Is this true?

Vociferous answered 12/12, 2011 at 3:9 Comment(2)
Here is an article on how to use the PageObject pattern in Python with Selenium: pragprog.com/magazines/2010-08/page-objects-in-pythonAgainst
A wayback machine link to the above dealink web.archive.org/web/20190428004331/https://pragprog.com/…Industrials
P
6

I don't think there is any equivalents of the Java annotations (@Find(By.xxx) etc) in Python. But it doesn't mean that you can't use the PageObject pattern.

You can find good example on how to do here : https://github.com/SeleniumHQ/selenium/blob/master/py/test/selenium/webdriver/common/google_one_box.py

Philae answered 12/12, 2011 at 15:53 Comment(2)
The link has changed to code.google.com/p/selenium/source/browse/py/test/selenium/…Britten
The link has again been updated to github.com/SeleniumHQ/selenium/blob/master/py/test/selenium/…Caldera
B
4

Dynamically-typed languages like Python are less obsessed by design patterns to create objects - because it is trivially easy just create object of any type (with proper methods) and return it. Patterns are common solutions to common problems. If something is not a problem, you don't need a pattern to deal with it :-) OOP was initially a design pattern in C.

Edit, Dec 2017:

In our homegrown framework for page automation (for automated UI testing and other purposes), we do use pageobject design pattern, but had no need for a page factory. Old-school inheritance from our custom BasePage covered all our (quite diversified) needs. We do use few tricks to create page elements and make sure that proper page was instantiated, and based on that experience I like that our PageObject is simple.

Also, Python allows for multiple inheritance, if your needs grow more complicated.

In my experience (using Python, Selenium and WebDriver for more than 5 years now), lack of page factory pattern is a sign that you don't need it, not that it cannot be implemented.

Bonefish answered 30/4, 2014 at 17:37 Comment(0)
C
2

I have created a module called pageobject_support that implements PageFactory pattern in a pythonic way.

With this module, Google Search page could be modelled as follows:

from pageobject_support import cacheable, callable_find_by as find_by
from selenium.webdriver.common.by.By

class GoogleSearchPage(object):

    _search_box = find_by(how=By.NAME, using='q', cacheable=True)

    _search_button = find_by(name='btnK')

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

    def search(self, keywords):
        self._search_box().click()
        self._search_box().send_keys(keywords)
        self._search_button().click()

Your feedback is appreciated. For more details, please visit https://jeremykao.wordpress.com/2015/06/10/pagefactory-pattern-in-python/

Conceited answered 10/6, 2015 at 14:15 Comment(0)
E
0

You can install the selenium-page-factory module.

https://selenium-page-factory.readthedocs.io/en/latest/

Evanston answered 26/7, 2023 at 19:6 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewIpecac

© 2022 - 2025 — McMap. All rights reserved.