当前位置:   article > 正文

selenium 选定ul-li下拉选项中某个指定选项_selenium 处理ul菜单li

selenium 处理ul菜单li

场景selenium的下拉选项是ul-li模式,选定某个指定的选项。
在这里插入图片描述

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC  # 显示等待
    def select_li(self, text, *ul_locator):
        """
        定位ul_li下拉选项中某个指定的下拉选项li
        :param text: 预期的输入项
        :param ul_locator: 定位到ul元素
        :return:
        """
        try:
            ul_ele = WebDriverWait(self.driver, self.WAIT_TIME, 1).until(EC.visibility_of_element_located((ul_locator)))
        except Exception as e:
            self.logger.error("ul元素在{}秒内定位失败: {}".format(self.WAIT_TIME, ul_locator))
        else:
            lis = ul_ele.find_elements('tag name', 'li')  # 拼接定位元素li
            for i in range(1, len(lis) + 1):
                # 拼接下拉选项的css locator的定位元素
                li = ul_locator[1] + ">li:nth-child(" + str(i) + ")>span"

                ele = WebDriverWait(self.driver, self.WAIT_TIME, 1).until(
                        EC.presence_of_element_located((By.CSS_SELECTOR, li)))
                if text in ele.text:  # 判断某个定位元素li的text是否是要选定的选项
                    ele.click()     # 点击下拉选项
					break
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/255937
推荐阅读
相关标签
  

闽ICP备14008679号