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
Python 在Selenium PageObject教程中,page.py文件和element.py文件是如何交互的?BasePageElement是什么?_Python_Selenium_Pageobjects - Fatal编程技术网

Python 在Selenium PageObject教程中,page.py文件和element.py文件是如何交互的?BasePageElement是什么?

Python 在Selenium PageObject教程中,page.py文件和element.py文件是如何交互的?BasePageElement是什么?,python,selenium,pageobjects,Python,Selenium,Pageobjects,基于用户在搜索引擎上的输入,我试图通过多个层次从网站上的搜索结果中提取数据。用户的搜索结果可能会有所不同,因此我决定使用PageObject设计模式来扩展我的项目,但我不了解Python Selenium教程以及element.py文件和page.py文件是如何交互的,因此我可以根据自己的喜好编辑它们 我将学习本教程: BasePageElement是什么?此文件是否仅适用于webscraping的第一层?我是否应该复制BasePageElement类并将其编辑到网站的下一层?对于element

基于用户在搜索引擎上的输入,我试图通过多个层次从网站上的搜索结果中提取数据。用户的搜索结果可能会有所不同,因此我决定使用PageObject设计模式来扩展我的项目,但我不了解Python Selenium教程以及element.py文件和page.py文件是如何交互的,因此我可以根据自己的喜好编辑它们

我将学习本教程:

BasePageElement是什么?此文件是否仅适用于webscraping的第一层?我是否应该复制BasePageElement类并将其编辑到网站的下一层?对于element.py文件实际执行的操作有更好的解释吗?

页面堆栈流示例

但目前的课程对我来说有两个问题:

BasePageElement只使用按名称查找元素,搜索表单中不同于字段的元素是无用的

BasePageElement在单个元素上搜索,所以在搜索页面上获取所有结果是无用的

它需要其他类来使它更有用

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException

# --- original classes ---

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 BasePageElement(object):
    """Base page class that is initialized on every page object class."""

    def __set__(self, obj, value):
        """Sets the text to the value supplied"""
        driver = obj.driver

        WebDriverWait(driver, 100).until(
            lambda driver: driver.find_element_by_name(self.locator))
        driver.find_element_by_name(self.locator).clear()
        driver.find_element_by_name(self.locator).send_keys(value)
        #driver.find_element_by_name(self.locator).send_keys(Keys.Return)

    def __get__(self, obj, owner):
        """Gets the text of the specified object"""
        driver = obj.driver
        WebDriverWait(driver, 100).until(
            lambda driver: driver.find_element_by_name(self.locator))
        element = driver.find_element_by_name(self.locator)
        return element.get_attribute("value")

# --- my classes ---

class SearchTextElement(BasePageElement):

    locator = 'q'

class MainPage(BasePage):

    # element has to be defined here 
    # (not as `self.field` because it will not work as expected)
    search_text_element = SearchTextElement()

    def search(self, query):
        self.search_text_element = [query, Keys.RETURN]

# ---- 

class ResultElement(BasePageElement):

    locator = 'q' # there is no other `name` on Stackoveflow

class ResultPage(BasePage):

    # element has to be defined here 
    # (not as `self.field` because it will not work as expected)
    result_element = ResultElement()

    def get_result(self):
        return self.result_element

# --- main ---

#driver = webdriver.Firefox()
driver = webdriver.Chrome()
driver.get('https://stackoverflow.com')

main_page = MainPage(driver)
main_page.search('selenium')

result_page = ResultPage(driver)
result = result_page.get_result()
print('result:', result)

我将复制BasePageElement或文件element.py并导入它,然后使用它创建自己的类SomeElementBasePageElement,而不更改原始BasePageElement。您甚至可以在链接类SearchTextElementBasePageElement中找到它。与类BasePage相同-复制它并使用它创建自己的类-即类MainPageBasePage:我觉得他们没有解释页面元素代码,所以我完全迷路了。类中的内容并不重要,每个类都需要不同的方法-这一切都取决于您需要什么。在BasePage中,您将在所有其他子类中放置所需的方法。在子类中,你可以放置你需要的方法来获取有趣的元素。它们为我们提供了一些例子http://www.python.org 但是对于其他页面,您需要在类中使用不同的方法。与BasePageElement类似,它有两个方法可用于所有其他子类。它使用self.locator来获取/设置值,在其他子类中,您必须添加它-例如,在类SearchTextElementBasePageElement中:您有locator='q'来获取搜索字段http://www.python.org -但在其他页面上,您可能需要不同的定位器。若你们需要访问不同的元素,那个么你们必须创建不同的子类。我用不同的方式来看待它。类定位器只保留“按元素查找”中使用的值。。。它没有使用这个值的函数。类元素获取定位器,并使用按元素查找元素。。。要找到它,请获取值或设置值。它还可以具有处理值、删除空格、转换为小写等功能。元素只能与页面上的一个元素一起工作。可以有两个不同的元素具有相同的定位器,但在不同的页面上使用它们。类页面不是定位器-它对页面上使用的许多元素进行分组。谢谢!我的代码差不多完成了。Keys.RETURN按钮仍然需要激活。我想页面对象设计模式终于点击了!谢谢你的帮助。