• js 操作还没试过,输入,我用的就是输入,输入后,日期弹框无法隐藏,主要是这个问题,至于点击,我觉得不太合适,因为日期,万一我以后要选其他日期怎么办?

  • 是上面两种不好处理,第三种倒简单了,定位输入框,输入,点击确定就可以搞定

    就这两种控件,输入日期,执行脚本的时候,没法让日期弹框隐藏,遮挡了其他元素的定位
    我刚想了一种办法,就是输入日期后,移动鼠标到输入框外面,然后点击,就可以隐藏日期弹框

    这是封装的移动鼠标的方法

    def move_by_offset(self,x,y):
        self.action.move_by_offset(x,y).perform()
    

    用例里面使用了这个移动鼠标的方法

    self.move_by_offset(100,0)
    

    但是现在的问题是,我怎么在移动鼠标后的位置进行点击
    刚试了在封装移动鼠标的方法后面直接加 click 不行

    def move_by_offset(self,x,y):
           self.action.move_by_offset(x,y).click.perform()
    
  • 我是在,写页面元素对象的页面,引入的 from selenium.webdriver.common.by import By
    元素定位的方式的话,我这也经常用的是 xpath, 但是我没有在 basepage 里去区分定位的方式,而是在这个页面,元素定位的时候加入了定位的方式

    from selenium.webdriver.common.by import By
    from basepage.BasePage import BasePage
    from selenium import webdriver
    import time
    
    class LoginPage(BasePage):
        #页面url
        url = ''
        #页面元素定位
        username = (By.XPATH, '//input[@placeholder="请输入用户名"]')
        password = (By.XPATH, '//input[@placeholder="请输入密码"]')
        #登录业务逻辑
        def login(self,usr,pwd):
            self.open(self.url)
            self.input(self.username,usr)
            time.sleep(2)
            self.input(self.password,pwd)
            time.sleep(2)
    
  • 好的,明白了

  • 我有几个疑问
    1、为啥要用 appium, MobileBy, 因为我这个是 webui 自动化框架,appium 不是搞移动端的吗?
    2、元素定位,xpath, 那要是其他的怎么办?id ,name

    MobileBy.XPATH
    
  • 那应该怎么写?

  • 多谢,现在明白了

  • 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 我用的隐式等待和强制等待,觉得不太好,打算改