• Selenium 中对于table的判断


    第一种:循环取出两个字段,一一进行判断,相等则做点击操作

      # elements = utils.find_elements(driver, By.CSS_SELECTOR, LSHTaxonomyLocators.LOC_TAXONOMY_TABLE_ROW)
        # # for row in elements:
        # #     if username in row.text and taxonomy_name in row.text:
        # #         action_element = row.find_element(By.CSS_SELECTOR, element_css)
        # #         action_element.click()
        # #         wait_for_window(1)
        # #         return
        # # table > tbody > tr:nth-child(2) > td > div
        #
        # size = len(elements)
        # i = 1
        # flag = 0
        # while i < size:
        #     row = utils.find_element(driver, By.CSS_SELECTOR, LSHTaxonomyLocators.LOC_TAXONOMY_ROW.format(i))
        #     print(row.text)
        #     name = utils.get_text(driver, By.CSS_SELECTOR,
        #                           "table > tbody > tr:nth-child({}) > td.list-name > div >span.list-name-text >div".format(
        #                               i))
        #     creator = utils.get_text(driver, By.CSS_SELECTOR,
        #                              "table > tbody > tr:nth-child({}) > td.list-lastModifiedBy > div".format(i))
        #     if taxonomy_name in row.text and creator.lower() == username.lower():
        #         flag = flag + 1
        #         print("This row {} is ok to choose".format(i))
        #         utils.find_element(driver, By.CSS_SELECTOR,
        #                            "table > tbody > tr:nth-child({}) > td > div >svg".format(i)).click()
        #
        #         # LOC_TAXONOMY_TABLE_ROW = "table.MuiTable-root.list-table > tbody > tr"
        #         # LOC_TAXONOMY_TABLE_ROW_STAR = "td > div > svg"
        #         print("This row {} is chosen successfully".format(i))
        #         utils.wait_for_window()
        #     i = i + 1
        # return flag
    

      

    第二种 table tr中,先取tr的值做判断,然后选中tr下的某某

    def find_row_n_click_element(driver, row_text, row_css, element_css):
        """Find table row and button in that row, click button"""
        action_rows = find_elements(driver, By.CSS_SELECTOR, row_css)
        for row in action_rows:
            if row_text in row.text:
                action_element = row.find_element(By.CSS_SELECTOR, element_css)
                action_element.click()
                wait_for_window(1)
                return
    
    
    def find_row_n_click_element_by2(driver, row_text, another_text, row_css, element_css):
        """Find table row and button in that row, click button"""
        action_rows = find_elements(driver, By.CSS_SELECTOR, row_css)
        flag = 0
        for row in action_rows:
            if row_text.lower() in row.text.lower() and another_text.lower() in row.text.lower():
                flag += 1
                action_element = row.find_element(By.CSS_SELECTOR, element_css)
                action_element.click()
                wait_for_window(1)
                return
        return flag
    

      

    第三种 常见的utils方法汇总

    now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    date = datetime.now().strftime("%d-%m-%Y")
    # ../../screenshot from current file dir
    path = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir))
    screen_dir = os.path.join(path, "screenshot", str(date))
    
    
    def screen_path():
        global screen_dir
        if not os.path.exists(screen_dir):
            os.makedirs(screen_dir)
        return screen_dir
    
    
    def save_screenshot(driver, name):
        driver.get_screenshot_as_file(os.path.join(screen_path(), name + '-' + now + ".png"))
    
    
    def get_url(driver, url):
        driver.get(url)
    
    
    def find_element(driver, by_type, locator, delay=3):
        try:
            return WebDriverWait(driver, delay).until(EC.presence_of_element_located((by_type, locator)))
    
        except TimeoutException:
            print("element {} was not found".format(locator))
    
    
    def find_elements(driver, by_type, locator, delay=3):
        try:
            return WebDriverWait(driver, delay).until(EC.presence_of_all_elements_located((by_type, locator)))
    
        except TimeoutException:
            print("element {} was not found".format(locator))
    
    
    def click(driver, by_type, locator):
        el = find_element(driver, by_type, locator)
        el.click()
    
    
    def select(driver, option, by_type, locator):
        _select = Select(find_element(driver, by_type, locator))
        _select.select_by_visible_text(option)
    
    
    def type_text(driver, text, by_type, locator):
        el = find_element(driver, by_type, locator)
        el.click()
        el.send_keys(text)
    
    
    def clear_text(driver, by_type, locator, delay=3):
        el = find_element(driver, by_type, locator, delay)
        el.click()
        el.clear()
    
    
    def type_text_press_enter(driver, text, by_type, locator):
        find_element(driver, by_type, locator).send_keys(text)
        find_element(driver, by_type, locator).send_keys(Keys.RETURN)
    
    
    def get_text(driver, by_type, locator):
        el = find_element(driver, by_type, locator)
        return el.text
    
    
    def switch_window(driver):
        wait_for_window(5)
        handles = driver.window_handles
        size = len(handles)
        for x in range(size):
            if handles[x] != driver.current_window_handle:
                driver.switch_to.window(handles[x])
    
    
    def wait_for_window(timeout=2):
        time.sleep(timeout)
    
    
    def wait_for_title(driver, title, timeout=20):
        try:
            WebDriverWait(driver, timeout).until(EC.title_contains(title))
        except TimeoutException as e:
            return
    
    
    def find_n_click(driver, button_text, css_selector):
        action_buttons = find_elements(driver, By.CSS_SELECTOR, css_selector)
        for button in action_buttons:
            if button_text in button.text:
                button.click()
                wait_for_window(1)
                break
    
    
    def find_row_n_click_button(driver, row_text, button_text, row_css, button_css):
        """Find table row and button in that row, click button"""
        action_rows = find_elements(driver, By.CSS_SELECTOR, row_css)
        for row in action_rows:
            if row_text in row.text:
                action_buttons = row.find_elements(By.CSS_SELECTOR, button_css)
                for button in action_buttons:
                    if button_text in button.text:
                        button.click()
                        wait_for_window(1)
                        return
    
    
    def find_row_n_click_element(driver, row_text, row_css, element_css):
        """Find table row and button in that row, click button"""
        action_rows = find_elements(driver, By.CSS_SELECTOR, row_css)
        for row in action_rows:
            if row_text in row.text:
                action_element = row.find_element(By.CSS_SELECTOR, element_css)
                action_element.click()
                wait_for_window(1)
                return
    
    
    def find_row_n_click_element_by2(driver, row_text, another_text, row_css, element_css):
        """Find table row and button in that row, click button"""
        action_rows = find_elements(driver, By.CSS_SELECTOR, row_css)
        flag = 0
        for row in action_rows:
            if row_text.lower() in row.text.lower() and another_text.lower() in row.text.lower():
                flag += 1
                action_element = row.find_element(By.CSS_SELECTOR, element_css)
                action_element.click()
                wait_for_window(1)
                return
        return flag
    
    
    def find_hower_n_click(driver, text, css_selector, hover_css_selector):
        rows = find_elements(driver, By.CSS_SELECTOR, css_selector)
        for row in rows:
            if text in row.text:
                view_button = row.find_element(By.CSS_SELECTOR, hover_css_selector)
                hover = ActionChains(driver).move_to_element(view_button)
                hover.perform()
                view_button.click()
                wait_for_window(1)
                break
    
    
    def refresh_page(driver):
        driver.refresh()
    
    
    ###############
    # GLOBAL FUNCTIONS #
    ###############
    
    
    def okta_login(driver, endpoint, username, password):
        print("==BEFORE METHOD==")
        print(endpoint)
        driver.get(endpoint)
    
        if ("LiveRamp Connect" in driver.title) & ("login" in driver.current_url):
            click(driver, By.LINK_TEXT, "Log in with identity provider")
            type_text(driver, "LiveRamp", By.ID, "lr-ui-input-input")
            click(driver, By.CSS_SELECTOR, "button.login-submit-button")
            wait_for_window()
    
        if "LiveRamp - Sign In" in driver.title or "Liverampsso - Sign In" in driver.title:
            type_text(driver, username, By.ID, "okta-signin-username")
            click(driver, By.ID, "okta-signin-password")
            type_text(driver, password, By.ID, "okta-signin-password")
            click(driver, By.ID, "okta-signin-submit")
    
        count = 0
        while ("Sign In" in driver.title) and (count < 5):
            wait_for_window()
            count += 1
        # close banners
        close_popup_banner(driver)
    
    
    def close_popup_banner(driver):
        dialog_button = find_element(driver, By.CSS_SELECTOR, 'button[id^="pendo-button"]')
        if dialog_button is None:
            dialog_button = find_element(driver, By.CSS_SELECTOR, 'button[id^="pendo-close"]')
        if dialog_button is not None:
            dialog_button.click()
    
    
    def global_chrome_options(chrome_options, mode):
        chrome_options.add_argument('--no-sandbox')
        if mode == 'headless':
            chrome_options.add_argument('--headless')
            chrome_options.add_argument('--disable-gpu')
            chrome_options.add_argument('--disable-dev-shm-usage')
            chrome_options.add_argument('window-size=1920x1480')
        return chrome_options
    
    
    def select_file_to_upload(driver, file_name, input_css_selector, connection="Connection", key="GCS"):
        local_name = file_name.rsplit("/", 1)[-1]
        f = open(os.path.abspath(os.getcwd()) + '/' + local_name, "wb")
        if connection == "Connection":
            parsers.CONNECTIONS[key].read_stream(f, file_name, None, None, gcp.PREFIX_RELIABILITY)
        elif connection == "Connection_LSH":
            """Support the connection to LSH GCP"""
            parsers.CONNECTIONS_LSH[key].read_stream(f, file_name, None, None, gcp.PREFIX_QUALITY)
        f.close()
        find_element(driver, By.CSS_SELECTOR, input_css_selector).send_keys(
            os.path.abspath(os.getcwd()) + '/' + local_name)
    

      

    ------------------------- A little Progress a day makes you a big success... ----------------------------
  • 相关阅读:
    javascript常用对象
    oracle储存过程,job,视图,触发器(记性不好,写个例子自己记)
    xml直接读取节点
    脑瓜子的文章导航 脑瓜子的学院系列文章汇总
    ASP.NET MVC 中实现View与Controller分离
    开发
    SQLite学习手册(转)
    缓存数据库redis、memcached。 MongoDB 资源集锦
    在GOOGLE浏览器中模拟移动浏览器 调试Web app
    Intelligencia.UrlRewriter在IIS 7.0下的完全配置攻略
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/14476717.html
Copyright © 2020-2023  润新知