首页 > 其他 > 详细

对button进行了显示等待依然不可点击的踩坑记录

时间:2021-08-30 09:34:29      阅读:27      评论:0      收藏:0      [点我收藏+]

背景:

使用企业微信进行自动化添加成员测试时,在通讯录中点击【添加成员】程序进入不到填写成员信息页面,其中也对该【添加成员】按钮进行了显式等待,依然无法进入成员信息页面。

初始代码实例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions


class TestContact:
    def setup(self):
        options = webdriver.ChromeOptions()
        options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
        # 这样写和上面的效果一样
        # self.options.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome(options=options)
        self.driver.get(‘https://work.weixin.qq.com/wework_admin/frame#index‘)
        self.driver.implicitly_wait(5)

    def teardown(self):
        self.driver.quit()

    def test_contact(self):
        self.driver.find_element(By.ID, ‘menu_contacts‘).click()
        # time.sleep(2)
        WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, ‘.js_has_member div:nth-child(1) .js_add_member‘)))
        self.driver.find_element(By.CSS_SELECTOR, ‘.js_has_member div:nth-child(1) .js_add_member‘).click()     # 点击添加成员
        self.driver.find_element(By.ID, ‘username‘).send_keys(‘Bob‘)        # 填写姓名

页面会报错:no such element

解决办法:

def test_contact(self):
    self.driver.find_element(By.ID, ‘menu_contacts‘).click()
    # time.sleep(2)
    WebDriverWait(self.driver, 10).until(self.wait_element)
    self.driver.find_element(By.ID, ‘username‘).send_keys(‘Bob‘)        # 填写姓名

def wait_element(self, x):
    # 如果没有进行添加成员信息页面,就一直循环点击
    size = len(self.driver.find_elements(By.ID, ‘username‘))
    if size < 1:
        self.driver.find_element(By.CSS_SELECTOR, ‘.js_has_member div:nth-child(1) .js_add_member‘).click()     # 点击添加成员
    return size >= 1

添加一个wait_element方法,结合WebDriverWait的until方法进行循环等待。

对button进行了显示等待依然不可点击的踩坑记录

原文:https://www.cnblogs.com/wangyha/p/15202248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!