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

How does the page.py file and the element.py file interact on the Selenium PageObject tutorial? What is the BasePageElement?

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

我正在学习本教程:https://selenium-python.readthedocs.io/page-objects.html

什么是 BasePageElement?这个文件是否只适用于网络抓取的第一层?我是否应该复制 BasePageElement class 并在我的网站抓取过程中为下一层编辑它?关于 element.py 文件实际做什么有更好的解释吗?

Stackoveflow

页示例

但是目前的 类 对我来说有两个问题:

  • BasePageElement 仅使用 find_element_by_name 并且在表单中搜索与字段不同的元素是无用的。

  • 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://whosebug.com')

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

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