python selenium 页面执行多种操作

1、选择下拉框

<div isteven-multi-select="" input-model="inChannelJson" output-model="outChannel" button-label="name" item-label="name" tick-property="ticked" max-labels="1" output-properties="value" translation="localLang" class="ng-isolate-scope">
  <span class="multiSelect inlineBlock buttonClicked">
    <button id="" type="button" ng-click="toggleCheckboxes( $event ); refreshSelectedItems(); refreshButton(); prepareGrouping; prepareIndex();" ng-bind-html="varButtonLabel" ng-disabled="disable-button" class="ng-binding">请选择
      <span class="caret"></span></button>
      <div class="checkboxLayer show">
      <div class="helperContainer ng-scope" ng-if="helperStatus.filter || helperStatus.all || helperStatus.none || helperStatus.reset ">
        <div class="line ng-scope" ng-if="helperStatus.all || helperStatus.none || helperStatus.reset ">
          <button type="button" class="helperButton ng-binding ng-scope" ng-disabled="isDisabled" ng-if="helperStatus.all" ng-click="select( 'all', $event );" ng-bind-html="lang.selectAll">✓全选</button>
          <button type="button" class="helperButton ng-binding ng-scope" ng-disabled="isDisabled" ng-if="helperStatus.none" ng-click="select( 'none', $event );" ng-bind-html="lang.selectNone">全不选</button>
        </div>
        <div class="line ng-scope" style="position:relative" ng-if="helperStatus.filter">
          <input placeholder="Search..." type="text" ng-click="select( 'filter', $event )" ng-model="inputLabel.labelFilter" ng-change="searchChanged()" class="inputFilter ng-valid ng-touched ng-dirty ng-valid-parse" style="width:90%; margin-right:10px;">
        </div>
      </div>
      <div class="checkBoxContainer">
        <div ng-repeat="item in filteredModel | filter:removeGroupEndMarker" class="multiSelectItem ng-scope vertical" ng-class="{selected: item[ tickProperty ], horizontal: orientationH, vertical: orientationV, multiSelectGroup:item[ groupProperty ], disabled:itemIsDisabled( item )}" ng-click="syncItems( item, $event, $index );" ng-mouseleave="removeFocusStyle( tabIndex );">
          <div class="acol">
            <label>
              <input class="checkbox focusable" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event, $index )">
              <span ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, 'itemLabel' )" class="ng-binding">AutoTest</span>
            </label>
          </div>
        </div>
      </div>
    </div>
  </span>
</div>
    def find_all_elements(self, time_out=10, *loc):
        """
        定位多个元素方法
        :param time_out: 超时时间 type: int
        :param loc: 元素定位 type: tuple
        :return:
        """
        try:
            # 等待元素出现
            WebDriverWait(self.driver, time_out).until(
                lambda driver: driver.find_element(*loc)
            )
            return self.driver.find_elements(*loc)
        except Exception as err:
            err_info = "\n{} 页面未找到 {} 元素 \n页面链接:{} \n{}".format(
                self.driver.title, loc, self.driver.current_url, err
            )
            log.error(err_info)

    def click_by_name(self, btn_loc, val):
        """
        根据按钮名称点击按钮
        :param btn_loc: 按钮元素 type: tuple (timeout, "xpath" xpath_str)
        :param val: 按钮名称 type: str
        :return:
        """
        try:
            # btn_loc[1]: 超时时间, btn_loc[0]: find_element 方法的 by 值, btn_loc[2]: 定位字符串
            btn_list = self.find_all_elements(int(btn_loc[1]), btn_loc[0], btn_loc[2])
            for btn in btn_list:
                if btn.text.strip() == val.strip():
                    btn.click()
                    break
        except AttributeError as err:
            err_info = "点击元素错误: {} \n{}".format(btn_loc, err)
            log.error(err_info)

2、获取列表标题栏的对应值

    def find_all_elements(self, time_out=10, *loc):
        """
        定位多个元素方法
        :param time_out: 超时时间 type: int
        :param loc: 元素定位 type: tuple
        :return:
        """
        try:
            # 等待元素出现
            WebDriverWait(self.driver, time_out).until(
                lambda driver: driver.find_element(*loc)
            )
            return self.driver.find_elements(*loc)
        except Exception as err:
            err_info = "\n{} 页面未找到 {} 元素 \n页面链接:{} \n{}".format(
                self.driver.title, loc, self.driver.current_url, err
            )
            log.error(err_info)

    def get_texts(self, title_loc, val_loc, index, title_val):
        """
        获取一组文本值对应的元素定位字典
        :param title_loc: 标题定位 type: tuple
        :param val_loc: 数据行定位 type: tuple
        :param index: 行数 type: int
        :param title_val: 标题值 type: str
        :return: text_dict
        """
        try:
            get_val = None
            text_list = self.find_all_elements(int(title_loc[1]), title_loc[0], title_loc[2])
            for num in range(len(text_list)):
                num_val = text_list[num].text.strip()
                if num_val == title_val:
                    new_path = val_loc[2].format(index, num + 1)
                    log.info("{}: {}".format(num_val, new_path))
                    get_val = self.get_text((val_loc[0], int(val_loc[1]), new_path))
                    break
            return get_val
        except Exception as err:
            err_info = "获取一组元素中的文本值错误: {}, {} \n{}".format(
                title_loc, val_loc, err
            )
            log.error(err_info)

3、页面执行多种操作

    def find_all_elements(self, time_out=10, *loc):
        """
        定位多个元素方法
        :param time_out: 超时时间 type: int
        :param loc: 元素定位 type: tuple
        :return:
        """
        try:
            # 等待元素出现
            WebDriverWait(self.driver, time_out).until(
                lambda driver: driver.find_element(*loc)
            )
            return self.driver.find_elements(*loc)
        except Exception as err:
            err_info = "\n{} 页面未找到 {} 元素 \n页面链接:{} \n{}".format(
                self.driver.title, loc, self.driver.current_url, err
            )
            log.error(err_info)

    def exec_actions(self, title_loc, val_loc, title_val, action, val=""):
        """
        执行操作,输入、点击、选择
        :param title_loc: 左侧名称定位 type: tuple
        :param val_loc: 操作定位 type: tuple
        :param title_val: 左侧名称 type: str
        :param action: 操作,包括"click", "input", "select" type: str
        :param val: 输入值 type: str
        """
        try:
            text_list = self.find_all_elements(int(title_loc[1]), title_loc[0], title_loc[2])
            for num in range(len(text_list)):
                num_val = text_list[num].text.replace(":", "").replace("\n", "").strip()
                if num_val == title_val:
                    new_path = val_loc[2].format(num + 1)
                    log.info("{}: {}".format(num_val, new_path))
                    if action == "click":
                        self.click_button((val_loc[0], int(val_loc[1]), new_path))
                    elif action == "input":
                        self.send_keys((val_loc[0], int(val_loc[1]), new_path), val)
                    elif action == "select":
                        self.select_combobox((val_loc[0], int(val_loc[1]), new_path), val)
                    break
        except Exception as err:
            err_info = "获取一组元素中的文本值错误: {}, {} \n{}".format(
                title_loc, val_loc, err
            )
            log.error(err_info)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容