pytest app自动化测试,click事件老是失败。

class TestCase1:
    def setup_class(self):
        global driver
        global page
        driver = base.base_driver.init_driver()
        page = Page(driver)

    def teardown_class(self):
        driver.quit()

    # --------------------------------------------
    # 首页
    # --------------------------------------------
    @pytest.mark.flaky(reruns=1, reruns_delay=3)
    @allure.story("============ 【首页】【所有元素点击】 ============")
    def test_story1(self):
        time.sleep(5)
        page.firstpage.is_ios_mode(15)

        # 点击 搜索
        page.firstpage.click_searchButton()
        time.sleep(2)
        page.firstpage.click_searchBack()
        time.sleep(2)

        # 点击 客服
        page.firstpage.click_kefuIcon()
        time.sleep(2)
        page.firstpage.click_kefuBack()
        time.sleep(2)
        # 点击 banner1
        page.firstpage.click_banner1()
        time.sleep(2)
        page.firstpage.click_banner1Back()
        time.sleep(2)
        # 点击 快报
        page.firstpage.click_fastNews()
        time.sleep(2)
        page.firstpage.click_fastNewsBack()

APP里的页面是webview嵌入的H5页面,
我必须在click前加time.sleep(2),不加老是失败,求解怎么才能避免这种方法

    def find_element(self, feature, timeout=20, poll=1):
        by = feature[0]
        value = feature[1]
        return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(by, value))

    def click(self, feature,timeout=20):
        self.find_element(feature,timeout).click()

我的方法是这么封装的,请问要怎么修改?

阅读 2k
1 个回答

应该是你的元素还没有加载完,你就去操作导致的,time.sleep(2)可以了是等待了以后,加载完了,你换个写法,比如写成

WebDriverWait(driver,time).until(lambda driver:driver.find_element(*loc).is_displayed())  

lambda的那种,会自动等待完成以后再去操作的那种,就不用手动写等待了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进