• 那应该怎么写?

  • 多谢,现在明白了

  • selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string
    
  • 我刚查了些资料,可以通过 cookie 绕过验证码,似乎也是可行的

  • 好的,我再继续把我的框架优化下


  • 楼主,像这种滑块验证,怎么弄

  • 嗯,元素定位的方法我写了很多,什么 id,name,等,粘贴的代码只粘贴了一个 xpath 的,确实,我写的时候好多定位都是 xpath, 你说的让脚本脱离代码环境,是说的数据驱动,关键字驱动吗,这个我有考虑做,后面计划是想做成这样

  • 还有,帮我看下,我这样写有啥问题吗,我现在感觉这样写,测试用例里面,取定位元素的方式,和值,那块有点繁琐,不知道能不能再优化

    base.find_element(loginpage.username[0],loginpage.username[1]).send_keys(username)
    

    就这一块

  • 好了,已经解决了上面的问题了,但是不知道为什么会启动两次浏览器,我再看看

  • def find_element(self, lc, locator,timeout=''):
        """元素定位"""
        if lc == "xpath":
            ele = WebDriverWait(self.driver, timeout=10)\
                .until(EC.visibility_of_element_located((By.XPATH,locator)))
    
    class Login_Page_Locator(BasePage):
        """登录页元素定位"""
        username = ('xpath', '//input[@placeholder="请输入用户名"]')
        password = ('xpath', '//input[@placeholder="请输入密码"]')
        submit = ('xpath','//span[text()="登录"]')
    
    class Test_Login(object):
        loginpage = Login_Page_Locator()
        base = BasePage()
        @allure.story("登录用例")
        @allure.severity(allure.severity_level.BLOCKER)
        def test_login1_normal(self):
            """username and passwd is normal"""
            Logger().info('test_login success')
            try:
                #输入用户名
                self.base.find_element(self.loginpage.username[0],self.loginpage.username[1]).send_keys(username)
            except NoSuchElementException:
                Logger().error('login no find element')
            #assert username == "admin"
            #insert_img("login_normal.png")
    

    为什么我这样写,一直在定位元素

  • 嗯是这样的

  • def __find_element__(self, lc, locator):
        """元素定位"""
        if lc == "id":
            #ele = self.driver.find_element(By.ID, locator)
            ele = WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.ID,locator)))
    
        return ele
    

    这样调整了一下,解决了

  • def __find_element__(self, lc, locator):
        """元素定位"""
        if lc == "id":
            ele = self.driver.find_element(By.ID, locator)
            print(type(ele))
            WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located(ele))
    
    <class 'selenium.webdriver.remote.webelement.WebElement'>
    Traceback (most recent call last):
      File "D:/demo_123/basic/page.py", line 48, in <module>
        a.__find_element__('id','kw').send_keys('python')
      File "D:/demo_123/basic/page.py", line 19, in __find_element__
        WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((ele)))
      File "D:\demo_123\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 81, in until
        value = method(self._driver)
      File "D:\demo_123\venv\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 125, in _predicate
        return _element_if_visible(driver.find_element(*locator))
    TypeError: find_element() argument after * must be an iterable, not WebElement
    
  • from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as ec
    
    class Page():
        def __init__(self):
            self.driver = webdriver.Chrome()
        def open(self):
            self.driver.get("https://www.baidu.com")
            self.driver.maximize_window()
    
        def __find_element__(self, lc, locator):
            """元素定位"""
            if lc == "id":
                ele = self.driver.find_element(By.ID, locator)
                print(type(ele))
                WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located(ele))
    
    a=Page()
    a.open()
    a.__find_element__('id','kw').send_keys('python')
    

    我打算重构的 basepage 页,简单调试了下,报了这个错

    <class 'selenium.webdriver.remote.webelement.WebElement'>
    Traceback (most recent call last):
      File "D:/demo_123/basic/page.py", line 48, in <module>
        a.__find_element__('id','kw').send_keys('python')
      File "D:/demo_123/basic/page.py", line 19, in __find_element__
        WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((ele)))
      File "D:\demo_123\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 81, in until
        value = method(self._driver)
      File "D:\demo_123\venv\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 125, in _predicate
        return _element_if_visible(driver.find_element(*locator))
    TypeError: find_element() argument after * must be an iterable, not WebElement
    
  • 好的

  • 一些常用的输入,点击,可以封装在基类页面吗

  • 嗯,我设计的确实这块问题很大,已经在重构了,打算封装一个基类 basepage 把启动浏览器,元素定位的类型,操作等单独封装起来


  • 这是在测试用例里面的加的 log 日志,断言,用例成功后的截图

  • https://blog.csdn.net/dream_back/article/details/118751500
    我找了一篇博客,看了别人封装的 po,觉得我现在的 po 有很大问题,定位元素的部分太过繁琐,而且还把业务的测试流程放在 page 里了

  • 看了,你的回复,我先修改下 ui 自动化的测试框架,UI 的问题,你说的 1,2,3, 2 和 3 有做,但是还是有问题,1 我用的隐式等待和强制等待,觉得不太好,打算改



  • 最近在研究这方面的东西,就是想知道,我写的这个跟真实的公司做自动化差别在哪里?

  • 看完你们就一个感觉,学历那么高的优势,怎么都在做测试,难道做开发不香,开发不是比测试薪资高多了吗?

  • 嗯,已经改了,现在好了

  • 好了,解决了,太感谢了

  • 好的,我试试