Example #1
0
    def swipe_find(self, text, limit=5):

        for i in range(limit):
            try:
                elemment = self.driver.find_element(MobileBy.XPATH, f"//*[@text='{text}']")
                self.driver.implicitly_wait(30)
                return elemment
            except NoSuchElementException:
                self.log_info(f"未找到{text}文本,继续滑动查找")
                size = self.driver.get_window_size()
                wid = size['width']
                height = size['height']
                start_x = wid / 2
                start_y = height * 0.8
                to_x = wid / 2 + wid * 1 / 9
                to_y = height * 0.3
                duration = 2000     # 单位毫秒
                self.driver.swipe(start_x, start_y, to_x, to_y, duration)

            if i == limit - 1:
                self.driver.implicitly_wait(30)
                raise NoSuchElementException(f"查找文本{text}{i}次后,未找到!!!")
Example #2
0
 def waitElement(self, by, value, secs=2):
     """
     等待元素显示
     """
     try:
         if by == "id":
             element = WebDriverWait(self.driver, secs, 0.5).until(
                 EC.presence_of_element_located((By.ID, value)))
             self.driver.execute_script("arguments[0].scrollIntoView()",
                                        element)
         elif by == "name":
             element = WebDriverWait(self.driver, secs, 0.5).until(
                 EC.presence_of_element_located((By.NAME, value)))
             self.driver.execute_script("arguments[0].scrollIntoView()",
                                        element)
         elif by == "class":
             element = WebDriverWait(self.driver, secs, 0.5).until(
                 EC.presence_of_element_located((By.CLASS_NAME, value)))
             self.driver.execute_script("arguments[0].scrollIntoView()",
                                        element)
         elif by == "link_text":
             element = WebDriverWait(self.driver, secs, 0.5).until(
                 EC.presence_of_element_located((By.LINK_TEXT, value)))
             self.driver.execute_script("arguments[0].scrollIntoView()",
                                        element)
         elif by == "xpath":
             element = WebDriverWait(self.driver, secs, 0.5).until(
                 EC.presence_of_element_located((By.XPATH, value)))
             self.driver.execute_script("arguments[0].scrollIntoView()",
                                        element)
         elif by == "css":
             element = WebDriverWait(self.driver, secs, 0.5).until(
                 EC.presence_of_element_located((By.CSS_SELECTOR, value)))
             self.driver.execute_script("arguments[0].scrollIntoView()",
                                        element)
         else:
             raise NoSuchElementException("找不到元素,请检查语法或元素")
     except TimeoutException:
         print("查找元素超时请检查元素")
Example #3
0
    def wait_visibility_one_of_elements(cls, elements: List[Union[WebElementProxy, WebElement]],
                                        timeout: Optional[int] = None,
                                        ticks: Optional[float] = 0.5) -> Union[WebElementProxy, WebElement]:
        """
        Ожидает пока один из переданных элементов станет видимым для пользователя (находится в DOM, имеет размер)
        :param elements: Список элементов для ожидания
        :param timeout: максимальное время ожидания
        :param ticks: частота проверок (по умолчанию - раз в пол секунды)
        :return:
        """
        if not elements:
            raise NoSuchElementException('Nothing to wait. At least one element must be passed')
        timeout = timeout or config.WEB_DRIVER_WAIT
        run_time = timeout

        while run_time > 0:
            for el in elements:
                if el.is_displayed():
                    return el
            time.sleep(ticks)
            run_time -= ticks
        raise ElementNotVisibleException('Could not wait for the visibility of any of transmitted elements')
Example #4
0
    def select_by_value(self, value):
        """Select all options that have a value matching the argument. That is, when given "foo" this
           would select an option like:

           <option value="foo">Bar</option>

           :Args:
            - value - The value to match against

           throws NoSuchElementException If there is no option with specified value in SELECT
           """
        css = "option[value =%s]" % self._escapeString(value)
        opts = self._el.find_elements(By.CSS_SELECTOR, css)
        matched = False
        for opt in opts:
            self._setSelected(opt)
            if not self.is_multiple:
                return
            matched = True
        if not matched:
            raise NoSuchElementException(
                "Cannot locate option with value: %s" % value)
Example #5
0
 def click_element(driver, xpath):
     """
     This clicks the WebElement found by given xpath
     :param: driver: webdriver
     :param: xpath: str
     """
     try:
         WebDriverWait(driver, 30, poll_frequency=1).until(
             EC.presence_of_element_located((By.XPATH, xpath)))
         driver.find_element_by_xpath(xpath).click()
     except TimeoutException:
         time.sleep(5)
         driver.find_element_by_xpath(xpath).click()
     except NoSuchElementException:
         raise NoSuchElementException(
             "Element not present with the given xpath")
     except StaleElementReferenceException:
         time.sleep(5)
         driver.find_element_by_xpath(xpath).click()
         Config.wait_for_page_load(driver)
     except ElementNotVisibleException:
         pass
Example #6
0
 def find_displayed_element(self, *locator):
     count_time = 0
     count_elements = len(self.driver.find_elements(*locator))
     while count_time < 200 and count_elements <= 0:
         sleep(0.1)
         count_elements = len(self.driver.find_elements(*locator))
         count_time += 1
     if count_elements == 1:
         element = self.driver.find_elements(*locator)[0]
         # WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(locator))
         if not element.is_displayed():
             count_time = 0
             while count_time < 200 and element.is_displayed() is False:
                 sleep(0.1)
                 count_time += 1
         if count_elements == 1 and not element.is_displayed():
             print("********!!!!!!!!!! Element found but not visible - ",
                   locator)
     elif count_elements == 0:
         raise NoSuchElementException(
             "********!!!!!!!!!! Element was not found - ", locator)
     return self.driver.find_elements(*locator)[0]
Example #7
0
 def element_wait(self, ele_type, value, secs=5):
     '''
     Waiting for an element to display.
     '''
     logger.info("wait for %d seconds." % secs)
     if ele_type == "id":
         WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.ID, value)))
     elif ele_type == "name":
         WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.NAME, value)))
     elif ele_type == "class":
         WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.CLASS_NAME, value)))
     elif ele_type == "link_text":
         WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.LINK_TEXT, value)))
     elif ele_type == "xpath":
         WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.XPATH, value)))
     elif ele_type == "css":
         WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.CSS_SELECTOR, value)))
     else:
         logger.error("NoSuchElementTypeException: %s" % ele_type)
         self.get_screenshot()
         raise NoSuchElementException(
             "Not find element, Please check the syntax error.")
Example #8
0
def get_locator(locators, ignore_implicit_wait=False, raise_exception=False):
    """
    :param locators: List of Dictionaries holding the locators, e.g. [{'id': 'elem_id'},
    {css: 'my_cls'}]
    :param ignore_implicit_wait: set it to True to set the implicit wait immediately to 0
    :param raise_exception: set it to True to get the NoSuchElementException in case no elements are matching any
    of the passed locators, if set to False then the first locator is returned in that case
    :return: first valid locator from the passed List, if no locator is valid then returns the
    first one
    """
    driver = _get_driver()
    timeout = _get_timeout()
    first_locator = None
    if ignore_implicit_wait:
        driver.implicitly_wait(0)
    for locator in locators:
        locator_type = list(locator.keys())[0]
        locator_value = locator[locator_type]
        if not first_locator:
            first_locator = (BYS[locator_type.lower()], locator_value)
        else:
            # set implicit wait to 0 get the result instantly for the other locators
            driver.implicitly_wait(0)
        elements = driver.find_elements(BYS[locator_type.lower()],
                                        locator_value)
        if len(elements) > 0:
            locator = (BYS[locator_type.lower()], locator_value)
            break
    else:
        if raise_exception:
            driver.implicitly_wait(timeout)
            msg = "Element not found: (%s, %s)" % first_locator
            raise NoSuchElementException(msg)
        else:
            locator = first_locator

    # restore the implicit wait value
    driver.implicitly_wait(timeout)
    return locator
    def wait_for(self, timeout, available=True, error=None):
        """
        Wait for a given element to become available.

        :param timeout: Time in seconds to wait for element.
        :type timeout: int
        :available: Used to check whether element is available or not available.
        :type available: bool
        :param error: Error message, if passed will raise NoSuchElementException.
        :type error: string, bool
        :return: Element, None
        """
        if not self.controller.wait(timeout=timeout, condition=self.check.available \
            if available else self.check.not_available):

            if error:
                raise NoSuchElementException(error if isinstance(error, string_types) else \
                    'Element by selector "{}" was {}'\
                    .format(self.selector, 'not found' if available else 'found'))
            return None

        return self
Example #10
0
    def swipe_find(self,num,text):
        for i in range(num+1):
            if i==num:
                self.driver.implicitly_wait(5)
                raise NoSuchElementException(f"找{num}次,未找到")
            self.driver.implicitly_wait(1)
            while True:
                try:
                    ele=self.driver.find_element(MobileBy.XPATH,f"//*[@text='{text}']")
                    self.driver.implicitly_wait(5)
                    return ele
                except:
                    print("未找到")
                    size = self.driver.get_window_size()
                    width = size.get("width")
                    height = size.get("height")
                    start_x = width/2
                    start_y = height*0.8
                    end_x = start_x
                    end_y = height*0.3

                    self.driver.swipe(start_x,start_y,end_x,end_y,1000)
Example #11
0
def get_element_when_visible_timeout(driver, xpath, timeoutsecs):
    visible = None
    n1 = dt.datetime.now()

    while (visible == None):
        try:
            element = driver.find_element_by_xpath(xpath)
            visible = element.is_displayed()
        except NoSuchElementException:
            #print (xpath + "is not visible");
            time.sleep(0.2)
            if (timeoutsecs > -1):
                n2 = dt.datetime.now()
                diff = (n2 - n1).seconds
                if (diff >= timeoutsecs):
                    raise NoSuchElementException('***Was waiting for ' +
                                                 str(diff) +
                                                 ' seconds but element at ' +
                                                 xpath + ' did not appear.')

    print(xpath + "is visible")
    return element
Example #12
0
def check_channel(driver, channel_name, option_name):
    str_xpath = "//*[contains(@placeholder,'{}')]".format(channel_name)
    driver.find_element_by_xpath(str_xpath).click()
    # 获取所有选项的频道名称
    list_elem = driver.find_elements_by_css_selector(
        ".el-select-dropdown__item span")
    is_suc = False
    # 对获取的频道名称进行遍历
    for i in list_elem:
        # 如果文本信息相等则点击,跳出
        if i.text == option_name:
            i.click()
            is_suc = True
            break
        # 如果不相等,则鼠标悬停当前元素,键盘点击向下
        else:
            action = ActionChains(driver)
            action.move_to_element(i).send_keys(Keys.DOWN).perform()
            is_suc = False
    # 判断标识符is_suc是否为False,如果是则抛出异常
    if is_suc is False:
        NoSuchElementException("没有找到{}元素".format(option_name))
Example #13
0
 def set_top_for_message(self, title, max_wait_time=5):
     locator = [
         MobileBy.XPATH,
         '//*[@resource-id="com.chinasofti.rcs:id/rl_conv_list_item" and ' +
         './/*[@resource-id="com.chinasofti.rcs:id/tv_conv_name" and @text="{}"]]'
         .format(title)
     ]
     self.find_message(title, max_wait_time)
     message = self.get_element(locator)
     position_x, position_y = message.location.get(
         'x'), message.location.get('y')
     self.mobile.tap([(position_x, position_y)], 1000)
     if self.is_text_present('取消置顶'):
         el = self.get_element([MobileBy.XPATH, '//*[@text="取消置顶"]'])
         position_y, position_y = el.location.get(
             'x'), el.location.get('y') - 100
         self.mobile.tap([(position_x, position_y)])
         return
     if self.is_text_present('置顶聊天'):
         self.click_text('置顶聊天')
     else:
         raise NoSuchElementException('没找到“置顶消息”菜单')
Example #14
0
 def __find_element(self, elem):
     """
     Find if the element exists.
     """
     for i in range(self.timeout):
         elems = Browser.driver.find_elements(by=elem[0], value=elem[1])
         if len(elems) == 1:
             logging.info("✅ Find element: {by}={value} ".format(
                 by=elem[0], value=elem[1]))
             break
         elif len(elems) > 1:
             logging.info(
                 "❓ Find {n} elements through: {by}={value}".format(
                     n=len(elems), by=elem[0], value=elem[1]))
             break
         else:
             sleep(1)
     else:
         error_msg = "❌ Find 0 elements through: {by}={value}".format(
             by=elem[0], value=elem[1])
         logging.error(error_msg)
         raise NoSuchElementException(error_msg)
Example #15
0
def checkSelection(self: WebDriver, form_id: str, select_name: str, value: str):
    """
    選択フォームに選択肢が存在するかをチェック.
    存在しなければNoSuchElementExceptionを返す

    Parameters
    ----------
    form_id : str
        対象フォームのid
    select_name : str
        フォーム内の<select>のname
    value : str
        選択肢<option>のvalue
    """
    try:
        option = self.find_element_by_xpath(
            "//form[@id='%s']//select[@name='%s']/option[@value='%s']" % (form_id, select_name, value))
    except NoSuchElementException as ex:
        pass
        raise NoSuchElementException(
            "フォーム %s の %s 欄の選択肢 %s が存在しません." % (form_id, select_name, value))
    return True
Example #16
0
    def get_element_with_text(self,
                              locator,
                              text,
                              params=None,
                              timeout=None,
                              visible=False):
        """
        Get element that contains <text> either by text or by attribute value.

        Note: if timeout is 0, this function will not wait for the element(s) to become present.

        :param locator: locator tuple or list of WebElements
        :param text: text that the element should contain
        :param params: (optional) locator parameters
        :param timeout: (optional) time to wait for text (default: self._explicit_wait)
        :param visible: (optional) if the element should also be visible (default: False)
        :return: WebElement instance
        """
        if timeout is None:
            timeout = self._explicit_wait

        @wait(exceptions=ElementNotVisibleException, timeout=timeout)
        def _wait_for_text():
            return self.is_element_with_text_present(locator, text, params,
                                                     visible)

        msg = "Element with type <{}>, locator <{}> and text <{text}> was never located!".format(
            *locator, text=text) if not isinstance(locator, list) else \
            "None of the elements had the text: {}".format(text)

        if timeout == 0:
            return self.is_element_with_text_present(locator, text, params,
                                                     visible)

        try:
            return _wait_for_text()
        except RuntimeError as e:
            LOGGER.debug(e)
            raise NoSuchElementException(msg)
Example #17
0
def extract_metrics(driver):
    """Scrape metrics to compute the rating for the property.

    Input:
        driver: Selenium webdriver

    Return:
        {
            Cleanliness: '5.0',
            Communication: '4.27',
            Check-in: '2.5',
            Accuracy: '4.5',
            Location: '2.5',
            Value: '4.8'
        }
    """
    d = {}

    try:
        metrics = driver.find_elements_by_css_selector(
            'div[data-section-id="REVIEWS_DEFAULT"] ._1s11ltsf')
        if len(metrics) == 0:
            raise NoSuchElementException("No metrics found for this property")
        for metric in metrics:
            criterion, score = metric.text.split("\n")
            d[criterion] = score
    except NoSuchElementException:
        for criterion in [
                "Cleanliness",
                "Communication",
                "Check-in",
                "Accuracy",
                "Location",
                "Value",
        ]:
            d[criterion] = "NA"

    return d
Example #18
0
    def __get_download_file_nameNlinks__(self):
        '''
        RETURN a list : [(file1, link1),(file2, link2), (file3,link3)...]
        '''
        _txt_dn_arrow = "//div[@class='atch_file_area']/a[@class='atch_view m-tcol-c']"
        _txt_dn_box = "//div[@class='atch_file_area']/div[@id='attachLayer']"
        _txt_cafe_main = "//div[@class='cafe_main']"
        _txt_files = "//div[@id='attachLayer']/ul/li/span[@class='file_name']"
        _txt_dn_links = "//div[@id='attachLayer']/ul/li/div[@id='attahc']/a[1]"
        _txt_dn_close = "//div[@class='ly_atch_file']/a[@class='clse']"
        self.switch_to.default_content()
        self.switch_to.frame('cafe_main')

        self.logger.debug("getting nameNlinks")

        time.sleep(1)
        # try:

        #     dn_box = self.find_element_by_xpath(_txt_dn_box)
        # except NoSuchElementException  :
        #    logging.error(str(self.__get_title__())+' :Download 게시물이 아닙니다.')

        if not self.__check_exists_by_xpath__(_txt_dn_box):
            raise NoSuchElementException("this hasn't DOWNLOAD FILED")

        self.find_element_by_xpath(_txt_dn_arrow).click()
        time.sleep(1)
        _links_ = self.find_elements_by_xpath(_txt_dn_links)
        _files_ = self.find_elements_by_xpath(_txt_files)
        time.sleep(1)

        _dn_links = [i.get_attribute('href') for i in _links_]
        _dn_files = [i.text for i in _files_]

        # Close download file box
        self.find_element_by_xpath(_txt_dn_close).click()
        time.sleep(1)
        return list(zip(_dn_links, _dn_files))
Example #19
0
    def check_channel_option(self, channel_name):
        # 获取区域元素的所在位置
        area_element = self.index_page.find_channel_area()
        x = area_element.location["x"]
        y = area_element.location["y"]

        # 获取区域元素的大小
        w = area_element.size["width"]
        h = area_element.size["height"]

        # 计算起始按住的滑动点的坐标
        start_y = y + h * 0.5
        start_x = x + w * 0.8

        # 计算目标位置的坐标
        end_y = start_y
        end_x = x + w * 0.2

        while True:
            # 先获取一次界面信息
            page_old = DriverUtils.get_app_driver()
            # 在当前区域中查找我们所想选择的频道元素对象
            try:
                # 如果能找到则点击
                self.index_page.find_channel_option(channel_name).click()
                break

            # 如果找不到则再次滑动页面
            except Exception as e:
                DriverUtils.get_app_driver().swipe(start_x, start_y, end_x,
                                                   end_y)

                # 在获取一次界面信息和滑动前的相等
                page_new = DriverUtils.get_app_driver().page_source

                # 如果滑动之后的页面信息和滑动之前的相等则跑出异常没有找到目标的选项
                if page_new == page_old:
                    raise NoSuchElementException("没有找到{}的频道")
Example #20
0
 def ferry_send_keys(cls, way, element, data):
     operate_result = {"status": "", "cell_color": "000000"}
     try:
         if way == "id":
             cls.dr.find_element_by_id(element).send_keys(data)
             operate_result = {"status": "pass", "cell_color": "00FF00"}
         if way == "name":
             cls.dr.find_element_by_name(element).send_keys(data)
             operate_result = {"status": "pass", "cell_color": "00FF00"}
         if way == "class_name":
             cls.dr.find_element_by_class_name(element).send_keys(data)
             operate_result = {"status": "pass", "cell_color": "00FF00"}
         if way == "tag_name":
             cls.dr.find_element_by_tag_name(element).send_keys(data)
             operate_result = {"status": "pass", "cell_color": "00FF00"}
         if way == "link_text":
             cls.dr.find_element_by_link_text(element).send_keys(data)
             operate_result = {"status": "pass", "cell_color": "00FF00"}
         if way == "partial_link_text":
             cls.dr.find_element_by_partial_link_text(element).send_keys(
                 data)
             operate_result = {"status": "pass", "cell_color": "00FF00"}
         if way == "xpath":
             cls.dr.find_element_by_xpath(element).send_keys(data)
             operate_result = {"status": "pass", "cell_color": "00FF00"}
         if way == "css_selector":
             cls.dr.find_element_by_css_selector(element).send_keys(data)
             operate_result = {"status": "pass", "cell_color": "00FF00"}
     except NoSuchElementException as e:
         my_log.error("没找到元素或页面元素未加载完全:%s" % e)
         raise NoSuchElementException(e)
     except ElementNotVisibleException as e:
         my_log.error("元素不可见:%s" % e)
         raise ElementNotVisibleException(e)
     except Exception as e:
         my_log.error("发生异常或错误:%s" % e)
         raise Exception(e)
     return operate_result
Example #21
0
def ig_login(driver,
             current_page=True,
             ig_email=IG_EMAIL,
             ig_password=IG_PASSWORD):
    """ Login to Instagram. This is required to view Story posts. """
    if not current_page:
        driver.get('https://www.instagram.com/accounts/login/')
    wait('quick')  # Let the page finish loading.
    # app.logger.info('============== InstaGram Login ===================')
    attempts, form_inputs = 5, []
    while attempts and not form_inputs:
        attempts -= 1
        try:
            form_inputs = driver.find_elements_by_css_selector('form input')
            if not form_inputs:
                raise NoSuchElementException('Not yet. ')
            app.logger.info(f"Form inputs found! On Attempt: {5 - attempts} ")
            app.logger.info(
                f"Have {len(form_inputs)} form inputs for ig_login. ")
        except NoSuchElementException as e:
            app.logger.info(f"Exception for target_button: {attempts} left. ")
            if not attempts:
                app.logger.error(e)
            else:
                wait('quick')
        except Exception as e:
            app.logger.error("Exception in ig_login. ")
            app.logger.error(e)
            driver.quit()
            raise e
    if form_inputs:
        email_input = form_inputs[0]
        password_input = form_inputs[1]
        email_input.send_keys(ig_email)
        password_input.send_keys(ig_password)
        password_input.send_keys(Keys.ENTER)
    success = len(form_inputs) > 0
    return driver, success
Example #22
0
    def select_by_visible_text(self, text):
        """Select all options that display text matching the argument. That is, when given "Bar" this
           would select an option like:

            <option value="foo">Bar</option>

           :Args:
            - text - The visible text to match against

            throws NoSuchElementException If there is no option with specisied text in SELECT
           """
        xpath = ".//option[normalize-space(.) = %s]" % self._escapeString(text)
        opts = self._el.find_elements(By.XPATH, xpath)
        matched = False
        for opt in opts:
            self._setSelected(opt)
            if not self.is_multiple:
                return
            matched = True

        if len(opts) == 0 and " " in text:
            subStringWithoutSpace = self._get_longest_token(text)
            if subStringWithoutSpace == "":
                candidates = self.options
            else:
                xpath = ".//option[contains(.,%s)]" % self._escapeString(
                    subStringWithoutSpace)
                candidates = self._el.find_elements(By.XPATH, xpath)
            for candidate in candidates:
                if text == candidate.text:
                    self._setSelected(candidate)
                    if not self.is_multiple:
                        return
                    matched = True

        if not matched:
            raise NoSuchElementException(
                "Could not locate element with visible text: %s" % text)
Example #23
0
    def __init__(self, name):

        # 路径是要运行的app安装路径+运行程序
        #
        # 远程启动参数browser browserName=chrome,chrome_binary="C:\Program Files (x86)\PoliceVP\PoliceVP.exe"

        if "remote" in name:
            http = name.split("=>")[1]
            self.driver = webdriver.Remote(
                command_executor=http,
                desired_capabilities=DesiredCapabilities.CHROME)
        elif "path" in name:
            path = name.split("=>")[1]
            _browser_url = path
            options = Options()
            options.binary_location = _browser_url
            options.add_argument("--headless")
            options.add_argument("--no-sandbox")
            options.add_argument("--disable-dev-shm-usage")
            # print(_browser_url+":二进制路径")
            # executablePath="D:\\ProgramFiles\\Python3.6.1\\chromedriver.exe"
            #executable_path=executablePath,
            self.driver = webdriver.Chrome(chrome_options=options)
            print("Driver为Null")
            # print("Driver不为Null")
            print("成功")
        elif name == "firefox":
            self.driver = webdriver.Firefox()
        elif name == "chrome":
            self.driver = webdriver.Chrome()
        elif name == "internet explorer" or name == "ie":
            self.driver = webdriver.Ie()
        elif name == "opera":
            self.driver = webdriver.Opera()
        elif name == 'edge':
            self.driver = webdriver.Edge()
        else:
            raise NoSuchElementException("没有找到应用")
Example #24
0
 def waitElemnet(self, selector_by, selector_value, secs=5):
     try:
         # 实在写不下去所有了,自己扩展出所有,一般感觉有这些够了
         if selector_by == "id":
             WebDriverWait(self.driver, secs, 1).until(
                 expected_conditions.presence_of_element_located(
                     (By.ID, selector_value)))
         elif selector_by == "name":
             WebDriverWait(self.driver, secs, 1).until(
                 expected_conditions.presence_of_element_located(
                     (By.NAME, selector_value)))
         elif selector_by == "link_text":
             WebDriverWait(self.driver, secs, 1).until(
                 expected_conditions.presence_of_element_located(
                     (By.LINK_TEXT, selector_value)))
         elif selector_by == "partial_link_text":
             WebDriverWait(self.driver, secs, 1).until(
                 expected_conditions.presence_of_element_located(
                     (By.PARTIAL_LINK_TEXT, selector_value)))
         elif selector_by == "xpath":
             WebDriverWait(self.driver, secs, 1).until(
                 expected_conditions.presence_of_element_located(
                     (By.XPATH, selector_value)))
         elif selector_by == "class_name":
             WebDriverWait(self.driver, secs, 1).until(
                 expected_conditions.presence_of_element_located(
                     (By.CLASS_NAME, selector_value)))
         elif selector_by == "css_selector":
             WebDriverWait(self.driver, secs, 1).until(
                 expected_conditions.presence_of_element_located(
                     (By.CSS_SELECTOR, selector_value)))
         else:
             logger.info(
                 "语法错误,参考: 'id=>caichang 或 xpath=>//*[@id='caichang'].")
             raise NoSuchElementException(
                 "语法错误,参考: 'id=>caichang 或 xpath=>//*[@id='caichang'].")
     except (TimeoutException, NoSuchElementException):
         logger.error("查找元素超时,请检查元素")
Example #25
0
 def wait_for_text(self,
                   value='',
                   by='xpath',
                   current_text='',
                   wait=10,
                   ignore=False):
     """
         Waits for the element to have a text value before continuing 
         @param value (string) - The string value of the xpath to use in the search
         @param by (string) - The type of action to perform, click or send_keys
         @param current_text (string) - What the current text is that we are waiting on to change 
         @param wait (int) - How long to wait for the text to show up
         @param ignore (bool) - If true will ignore the fact the text never changed,
         if false will expect text to load
         @throws NoSuchElementException if text attribute is still empty
         @author CodyC
         @date 8/3/13
         @par EXAMPLE
         driver.wait_for_text("//input[contains(@id,"FirstNameField")]", by="xpath")
     """
     logger.info("Waiting for Field to populate with text")
     start = time()
     el = self.element_find(value, by, action="None")
     while el.get_attribute("value") == current_text:
         if time() - start > wait:
             break
         else:
             el = self.element_find(value, by, action="None")
     logger.info(
         "Checking if value of input is not empty (expects not to be empty)"
     )
     el = self.element_find(value, by, action="None")
     if el.get_attribute("value") == current_text:
         if ignore:
             logger.error("Text never changed")
         else:
             raise NoSuchElementException(
                 "input field is %s, expecting it to change" % current_text)
Example #26
0
def first_from(*locs, **kwargs):
    """ Goes through locators and first valid element received is returned.

    Useful for things that could be located different way

    Args:
        *locs: Locators to pass through
        **kwargs: Keyword arguments to pass to element()
    Raises:
        NoSuchElementException: When none of the locator could find the element.
    Returns: :py:class:`WebElement`
    """
    assert len(locs) > 0, "You must provide at least one locator to look for!"
    for locator in locs:
        try:
            return element(locator, **kwargs)
        except NoSuchElementException:
            pass
    # To make nice error
    msg = locs[0] if len(locs) == 1 else ("%s or %s" %
                                          (", ".join(locs[:-1]), locs[-1]))
    raise NoSuchElementException(
        "Could not find element with possible locators %s." % msg)
Example #27
0
def find_element_and_verify_visible(locator, max_number_of_tries=1):
    s2l = get_s2l()

    for i in range(max_number_of_tries):
        try:
            element = s2l._element_find(locator, True, False)

            if element is None:
                raise NoSuchElementException(
                    "Unable to find element with locator: %s" % locator)

            if s2l.is_element_visible(element):
                logger.info("Element %s verified as visible" % locator)
            else:
                fail_test("Element %s NOT visible" % locator)
        except StaleElementReferenceException:
            continue
        else:
            break
    else:
        raise StaleElementReferenceException(
            "Element %s not attached to DOM after %s try(s)" %
            (locator, max_number_of_tries))
Example #28
0
    def deselect_by_value(self, value):
        """Deselect all options that have a value matching the argument. That is, when given "foo" this
           would deselect an option like:

            <option value="foo">Bar</option>

           :Args:
            - value - The value to match against

            throws NoSuchElementException If there is no option with specisied value in SELECT
        """
        if not self.is_multiple:
            raise NotImplementedError(
                "You may only deselect options of a multi-select")
        matched = False
        css = "option[value = %s]" % self._escapeString(value)
        opts = self._el.find_elements(By.CSS_SELECTOR, css)
        for opt in opts:
            self._unsetSelected(opt)
            matched = True
        if not matched:
            raise NoSuchElementException(
                "Could not locate element with value: %s" % value)
Example #29
0
def init_driver():
    try:
        username = '******'
        driver = Chrome()
        driver.implicitly_wait(20)
        driver.get(url=r'https://cs1.jsbooks.com.cn/user/login')
        driver.maximize_window()
        driver.find_element_by_xpath(
            '//*[@id="login"]//input[@placeholder="请输入手机号"]').send_keys(
                username)
        driver.find_element_by_xpath(
            '//*[@id="login"]//span/span/span').click()
        driver.find_element_by_xpath(
            '//*[@id="login"]//input[@placeholder="请输入短信验证码"]').send_keys(
                '123456')
        driver.find_element_by_xpath(
            '//*[@id="login"]/div[2]/div/div/div[3]/label/span').click()
        driver.find_element_by_xpath(
            '//*[@id="login"]/div[2]//button[@type="button"]').click()
        yield driver
        driver.quit()
    except:
        raise NoSuchElementException("登录客户端失败")
Example #30
0
    def enter_words(self, id_xpath_area, words, id_xpath_button=None, t=0.5):
        """Функция для заполнения полей по ID или XPATH
           При необходимости нажатие на кнопку. По-умолчанию без нажатия"""
        try:
            if id_xpath_area.startswith('//*[@'):
                self.driver.find_element_by_xpath(id_xpath_area).clear()
                self.driver.find_element_by_xpath(id_xpath_area).send_keys(
                    words)
            else:
                self.driver.find_element_by_id(id_xpath_area).clear()
                self.driver.find_element_by_id(id_xpath_area).send_keys(words)
            time.sleep(t)

            if id_xpath_button:
                if id_xpath_button.startswith('//*[@'):
                    self.driver.find_element_by_xpath(id_xpath_button).click()
                else:
                    self.driver.find_element_by_id(id_xpath_button).click()
            time.sleep(t)
        except NoSuchElementException:
            raise NoSuchElementException(
                'Вы находитесь в специфическом окне, где нельзя что либо вставить или нажать'
                'кнопку, может быть ошибка в пути')
Example #31
0
def main(OutputFileName="ITTCourseList.csv", FileDelimiter=";", GetCoursesFromURL='http://courses.it-tallaght.ie/',DeptListDivID='homeProgrammes', WebPageLoadDelay=10):
    # Function Parameters for IT-Tallaght  OutputFileName="ITTCourseList.csv", FileDelimiter=";", GetCoursesFromURL='http://courses.it-tallaght.ie/',DeptListDivID='homeProgrammes', WebPageLoadDelay=10
    # Function Parameters for IT-Blanch Course: OutputFileName="ITBlanchCourseList.csv", FileDelimiter=";", GetCoursesFromURL='http://courses.itb.ie/',DeptListDivID='homeProgrammesWide', WebPageLoadDelay=10
    Spacer ="\n------File Writer------\n"
    TextContentsFileName ="Text/"
    # Create files to store the output in (w)rite mode and add the header to the FileDelimiter specified in the function parameters 
    MyCSVFile = open(OutputFileName, "wb")    
    CourseList = csv.writer(MyCSVFile, delimiter=FileDelimiter)
    # Write the 1st row to give the column names
    CourseList.writerow(['Dept', 'link', 'CourseName','CourseAward', 'CourseCode', 'CourseDelivery', 'SemesterCount', 'CourseNFQLevel', 'CourseDepartment']) 
    URLToParse = GetCoursesFromURL
    # Open the webpage using 
    WebContent = urllib2.urlopen(URLToParse)
    #Parse the content using soup but strip out non ascii chars first
    soup = BeautifulSoup(WebContent, "html.parser")
    # Open the webpage using selenium
    driver = webdriver.Firefox()
    # Give the page time to load before continuing by waiting 5 seconds
    driver.implicitly_wait(WebPageLoadDelay)  # seconds
    print('Trying to parse ', URLToParse ,' now')
    driver.get(URLToParse)
    subset = driver.find_element_by_id(DeptListDivID)
    # Just get the part of the document that contains the list of department #  xpath //*[(@id = "homeProgrammes")] contains the list of departments but just need the id field here
    print('Finding the DIV Id', DeptListDivID, " on the webpage")
    Depts = soup.find(id=DeptListDivID)
    # print("Print out the nicely formatted Unicode with tags on their own line")
    #print(soup.prettify())
    # print("Print just the part of the doc where the id homeProgrammes was found")
    # print(Depts)
    for links in Depts.findAll('a'): 
            print(links)
            # print("--------SPACER-----------------")
            print('Processing Department ',links.string,' link(s) now')
            # Using selenium find the link to the depts list of courses that matches the link string from beautiful soup and click it
            FollowLink = subset.find_element_by_link_text(links.string)
            FollowLink.click()
            # Try waiting 10 seconds for the element with ID 'ProgrammeListForDepartment' is available 
            try: 
                # Get the Overlay i.e the list of the course in the div ProgrammeListForDepartment (it could also be homeProgrammesWide so check the webpage source and use the appropriate parameter 
                Overlay = WebDriverWait(driver, WebPageLoadDelay).until(EC.presence_of_element_located((By.ID, "ProgrammeListForDepartment")))
                # Get it as a Beautiful soup object too as its easier to read
                SoupOverlay = BeautifulSoup(Overlay.get_attribute('outerHTML'), "html.parser")
                #print(Soup.prettify())
                # close the overlay
                Overlay.find_element_by_link_text("close").click()
            except NoSuchElementException: 
                print(NoSuchElementException.msg())
                # Exit now
                sys.exit(1)
                # loop over the links 
            for courselink in SoupOverlay.findAll('a'): 
                if courselink.get('href') != "":  
                    FullLink = URLToParse + courselink.get('href')
                    # Add them to the file
                    # = [links.string, courselink.get_text(), FullLink];
                    print("--Found these non blank urls--")
                    print("Dept: ", links.string, " link ",FullLink," Course Name", courselink.getText())
                    #Parse the course link itself and its child modules
                    print('Getting the course details and module text for ',courselink.getText()," now")
                    CourseContentsDictionary = ParseCoursePage.main(FullLink, URLToParse)
                    print("Got the following keys", CourseContentsDictionary.keys(), " back from the parsing function")
                    #Use the Coursecode as the unique filename
                    TextContentsFileName = CourseContentsDictionary['CourseCode']
                    #Get the non-unicode value so u'CourseCode' don't corrupt the html when its printed to file
                    TextContentsFileName = str(TextContentsFileName.strip())
                    #Create a file with utf-8 encoding
                    MyHTMLFile = codecs.open(TextContentsFileName+".html", "w",encoding='utf-8')
                    HeaderText = "<h1> Course Outcomes for "+ TextContentsFileName +"</h1>"
                    MyHTMLFile.write(HeaderText)
                    #Add html div tags to the CourseOutcomes text and include an ID value for equal measure
                    EncasedCourseOutcomes = "<div id=",TextContentsFileName,">",CourseContentsDictionary['CourseOutcomes'],"</div>"
                    MyHTMLFile.write(EncasedCourseOutcomes.__str__()) 
                    MyHTMLFile.write("<h1> Module Content </h1>")
                    MyHTMLFile.write(CourseContentsDictionary['CourseModuleText']) 
                    print("Writing the Module contents for ",TextContentsFileName," to file")
                    # Write the results to the file after calling the ParseCoursePage function to pull the data from that page and the module pages linked to it
                    print('Writing ', courselink.getText(), 'to file','TextContentsFile')
                    #CourseList. Row Structure (['Dept', 'link', 'CourseName','CourseAward', 'CourseCode', 'CourseDelivery', 'SemesterCount', 'CourseNFQLevel', 'CourseDepartment', 'CourseOutcomes', 'CourseModuleText']) 
                    CourseList.writerow([links.string, FullLink, courselink.getText(),CourseContentsDictionary['CourseAward'] ,CourseContentsDictionary['CourseCode'],CourseContentsDictionary['CourseDelivery'], CourseContentsDictionary['SemesterCount'] ,CourseContentsDictionary['CourseNFQLevel'] ,CourseContentsDictionary['CourseDepartment']])
                    MyCSVFile.flush()       
    # Close the csv file
    print('File', MyCSVFile.name ,' closed')
    MyCSVFile.close
    MyHTMLFile.close()
    driver.close()
    print('External Web browser closed')
    # Exit successfully
    sys.exit(0)