Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open()报错 #41

Closed
kafkaliang opened this issue Jan 6, 2023 · 3 comments
Closed

open()报错 #41

kafkaliang opened this issue Jan 6, 2023 · 3 comments

Comments

@kafkaliang
Copy link

kafkaliang commented Jan 6, 2023

想问一下,这个源码中没有找get是什么原因呢?

    def setUp(self) -> None:
>       LP.open(WebConfig.url)

test_1_Login.py:12: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <PageObjects.LoginPage.LoginPage object at 0x0000023DB1D6C3D0>
uri = 'https://send2boox.com'

    def open(self, uri):
        """
        :param uri:  URI to GET, based off of the root_uri attribute.
        """
        root_uri = self.root_uri or ''
>       self.driver.get(root_uri + uri)
E       AttributeError: 'function' object has no attribute 'get'

C:\Users\keeponzhang\AppData\Local\Programs\Python\Python38\lib\site-packages\poium\selenium.py:79: AttributeError

@defnngj
Copy link
Collaborator

defnngj commented Feb 9, 2023

LP 需要驱动

from selenium import webdriver
from poium import Page, Element, Elements

# page层定义
class BaiduPage(Page):
    ...

dr = webdriver.Firefox()
LP = BaiduPage(dr)  # <--- LP 需要 驱动 `dr`
LP.open("https://www.baidu.com")

@kafkaliang
Copy link
Author

kafkaliang commented Feb 14, 2023

是的,后来查了一下,加上继承就好了

from poium import Page, Element
from Conf.dataConfig import *


class LoginPage(Page):
    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver

@kafkaliang
Copy link
Author

不过其实这里我有个疑问,LoginPage继承的是Page

class LoginPage(Page):
    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver

Page又是继承的PageBase,Pagebase里面有这一段,为啥LoginPage还要driver?

class PageBase(object):
    """
    Page Object pattern.
    """

    def __init__(self, driver, url=None,  print_log: bool = False):
        """
        :param driver: `selenium.webdriver.WebDriver` Selenium webdriver instance
        :param url: `str`
        :param print_log: `bool` Need to be turned on when used with the seldom framework
        Root URI to base any calls to the ``PageObject.get`` method. If not defined
        in the constructor it will try and look it from the webdriver object.
        """
        self.driver = driver
        self.root_uri = url if url else getattr(self.driver, 'url', None)
        config.printLog = print_log

是因为conf里面的driver = None吗?

class Browser:
    # Default browser driver
    driver = None

那我下面这样处理可以吗?

import unittest
from selenium import webdriver


class TestDriverClass(unittest.TestCase):
    option = webdriver.ChromeOptions()
    # 设置为无头模式运行
    option.add_argument("--headless")
    # 设置每个用例运行后不关闭浏览器
    option.add_experimental_option("detach", True)
    option.add_argument(
        f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36')
    # 指定driver路径
    driver = webdriver.Chrome(options=option, executable_path=r'../driver_tools/chromedriver.exe')

    def setUp(self, driver=driver) -> None:
        self.driver = driver
        windows_size = self.driver.get_window_size('current')
        print(windows_size)
        # 有头模式下设置窗口大小
        # self.driver.maximize_window()
        # 无头模式下设置
        self.driver.set_window_size(1300, 800)

    def tearDown(self, driver=driver) -> None:
        self.driver = driver
        self.driver.refresh()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants