Esempio n. 1
0
def main():

    if len(sys.argv) != 2 or sys.argv[1] not in ('proxy', 'node'):
        exit('usage: driver_test.py <proxy|node>')

    is_proxy = sys.argv[1] == 'proxy'

    if is_proxy:
        setup_proxy_mode()
        base_url = 'http://' + PROXY_IP + ':5000/driver'
    else:
        setup_node_mode()
        base_url = 'http://' + NODE_IP + ':5555'

    time.sleep(7)

    driver = Remote(base_url + '/wd/hub', desired_capabilities=CAPABILITIES)
    driver.get('http://soas.ac.uk')
    links = driver.find_elements_by_xpath('//a')
    curr_link = links[1]
    outer_html = curr_link.get_attribute('outerHTML')
    import pdb
    pdb.set_trace()
    print('result:')
    print(outer_html)
Esempio n. 2
0
    def _get_captcha_svg(self, browser: webdriver.Remote) -> str:
        web_elements = browser.find_elements_by_xpath(
            '//div[@class="col-sm-6"]/div[2]/*[name()="svg"]/*[name()="path"]')

        html_elements = map(lambda element: element.get_attribute('outerHTML'),
                            web_elements)

        purified_svg_elements = filter(
            lambda element: 'fill="none"' not in element, html_elements)

        svg_image = ""
        svg_image += ('<svg xmlns="http://www.w3.org/2000/svg" ' +
                      'width="150" height="50" viewBox="0,0,150,50">')
        for element in purified_svg_elements:
            svg_image += element
        svg_image += "</svg>"

        return svg_image
Esempio n. 3
0
def get_elem(driver: webdriver.Remote, by, locator, timeout):
    found = 0
    if by not in ["id", "css", "class", "xpath"]:
        raise Exception("invalid by locator type passed to {}".format("get_elem"))
    start_time = int_time()
    while found == 0:
        time.sleep(1)
        if (int_time() - start_time) > timeout:
            raise TimeoutError("couldn't find element {}:{} in {}s".format(by, locator, timeout))
        if by == "id":
            elements = driver.find_elements_by_id(locator)
        if by == "css":
            elements = driver.find_elements_by_css_selector(locator)
        if by == "class":
            elements = driver.find_elements_by_class_name(locator)
        if by == "xpath":
            elements = driver.find_elements_by_xpath("/html/body/div[2]/div[2]/div/form/input[5]")
        if elements == None:
            logger.debug("no elements found, trying again...")
            continue
        found = len(elements)
        if found == 0:
            logger.debug("no elements found, trying again...")
    return elements[0]
Esempio n. 4
0
class BasePage(object):
    def __init__(self, project_name):
        self.driver = ''
        self.loc = ''
        self.project_name = project_name
        yamlPath = "user.yaml"
        yaml.load(yamlPath, Loader=yaml.BaseLoader)
        yaml.warnings({'YAMLLoadWarning': False})
        f = open(yamlPath, 'r')
        temp = yaml.load(f.read())
        self.url = temp[project_name]['url']
        self.username = temp[project_name]['username']
        self.password = temp[project_name]['password']

    def open(self,
             browser="chrome",
             host='http://localhost:8081/wd/hub'
             ):  # 初始化 打开浏览器 并最大化  self 与java中的this中一样,调用时不用传入self参数
        try:
            self.driver = Remote(command_executor=host,
                                 desired_capabilities={
                                     'platform': 'ANY',
                                     'browserName': browser,
                                     'version': '',
                                     'javascriptEnabled': True
                                 })
            self.driver.maximize_window()
        except Exception as e:
            print(e)

        logger_cls.info(u"打开{0}浏览器".format(browser))
        logger_cls.info(u"最大化")

    def get(self):
        try:
            self.driver.get(self.url)
            self.driver.implicitly_wait(10)  # 隐性等待,最长等10秒
            logger_cls.info(u'打开:{0}'.format(self.url))
        except BaseException:
            logger_cls.error(u'打开{0}失败'.format(self.url))
        self.loc = loc.Analyze(self.project_name)  #初始化,读取xml 赋值给loc

    def find(self, name):  #元素定位,并返回定位好的元素
        try:
            el = WebDriverWait(self.driver, 3,
                               0.5).until(  #设置显示等待时间,每0.5秒检查一次,如果超出指定值就报错
                                   EC.presence_of_element_located(
                                       (self.loc[name].type,
                                        self.loc[name].UIIdentifier)))
            logger_cls.info(u'定位元素:{0}'.format(name))
            # logger_cls.info(loc[name].value)
        except BaseException:
            logger_cls.error(u'定位元素:{0}失败'.format(name))
        return el

    def send_keys(self, name, text):
        try:
            self.find(name).send_keys(text)
            logger_cls.info(u'在:{0}输入{1}'.format(name, text))
            time.sleep(3)
        except BaseException:
            logger_cls.error(u'在:{0}输入{1}失败'.format(name, text))

    def click(self, name):
        try:
            self.find(name).click()
            logger_cls.info(u'点击:{0}'.format(name))
            time.sleep(3)
        except BaseException:
            logger_cls.error(u'点击:{0}失败'.format(name))

    def being(self, name):
        t = False
        try:
            self.driver.find_element_by_xpath(self.loc[name].UIIdentifier)
            t = True
            logger_cls.info(u'{0}元素存在'.format(name))
        except BaseException:
            logger_cls.info(u'{0}元素不存在'.format(name))
        return t

    def login(self):
        self.get()
        self.send_keys(u'用户名', self.username)
        self.send_keys(u'密码', self.password)
        self.click(u'登录')
        time.sleep(3)
        if self.being(u'不再提示'):
            self.click(u'不再提示')
        # self.get_version()

    def clearmonitor(self):
        names = [u'博主', u'博主圈']
        self.click(u'事件')
        if self.being(u'是否有事件'):
            self.focus(u'找回')
            self.click(u'多选')
            self.click(u'全选')
            self.click(u'删除')
        for name in names:
            self.click(name)
            if self.being(u'是否有博主'):
                self.focus(u'找回')
                self.click(u'多选')
                self.click(u'全选')
                self.click(u'删除')

    def randomclick(self, name, div=None):
        text = ''
        i = len(self.driver.find_elements_by_xpath(
            self.loc[name].UIIdentifier))
        logger_cls.info(u'{0}列表中有{1}个参数'.format(name, i))
        y = random.randint(1, i)
        if div == None:
            path = self.loc[name].UIIdentifier + '[' + str(y) + ']'
            text = self.driver.find_element_by_xpath(path).text
            self.driver.find_element_by_xpath(path).click()
            logger_cls.info(u'随机选择列表中的{0}第个参数并点击'.format(y))
        else:
            i2 = len(
                self.driver.find_elements_by_xpath(
                    self.loc[name].UIIdentifier + '[' + str(y) + ']'))
            y2 = random.randint(1, i2)
            path = self.loc[name].UIIdentifier + '[' + str(y2) + ']' + div
            text = self.driver.find_element_by_xpath(path).text
            self.driver.find_element_by_xpath(path).click()
            logger_cls.info(u'随机选择列表中的{0}第个参数并点击'.format(y2))

        logger_cls.info(u'{0}:文本的值为:{1}'.format(name, text))
        time.sleep(3)
        return text

    def close(self):
        logger_cls.info(u'3秒后关闭当前页面')
        time.sleep(3)
        self.driver.close()

    def quit(self):
        logger_cls.info(u'3秒后关闭浏览器')
        time.sleep(3)
        self.driver.quit()

    def get_url(self):
        url = self.driver.current_url
        logger_cls.info(u'当前页面url:' + url)
        return url

    def get_text(self, name):
        text = self.find(name).text
        logger_cls.info(u'{0}文本框的值为:{1}'.format(name, text))
        return text

    def back(self):
        self.driver.back()
        logger_cls.info(u'返回上一页面')

    def clear(self, name):
        self.find(name).clear()
        logger_cls.info(u'清空文本框:{0}'.format(name))

    def get_name(self):
        name = self.driver.name
        logger_cls.info(u'浏览器名称:{0}'.format(name))

    def get_driver(self):
        return self.driver

    def get_version(self):
        version = self.driver.capabilities['version']
        logger_cls.info(u'浏览器版本:{0}'.format(version))
        return version

    def switch_to(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
        logger_cls.info(u'切换页面')

    def focus(self, name):
        ele = self.find(name)
        ActionChains(self.driver).move_to_element(ele).perform()
        logger_cls.info(u'鼠标悬停到元素:{0}'.format(name))

    def refresh(self):
        self.driver.refresh()
        logger_cls.info(u'刷新页面')

    def title(self):
        title = self.driver.title
        logger_cls.info(u'当前页面标题' + title)
        return title

    def Slide(self, height):
        js = "var q=document.documentElement.scrollTop={0}".format(str(height))
        self.driver.execute_script(js)
        logger_cls.info(u'上下滑动' + str(height))

    def sleep(self, i):
        logger_cls.info(u'暂停{0}秒'.format(i))
        time.sleep(int(i))

    def Screenshot(self, name):
        # name='screenshot_'
        isExists = os.path.exists("../images\\")
        # 判断结果
        if not isExists:
            # 如果不存在则创建目录
            # 创建目录操作函数
            os.makedirs("../images\\")
            print u'创建images目录'
        timestrmap = time.strftime('%Y%m%d_%H%M%S')
        imgPath = os.path.join('../images\\', str(timestrmap) + name + '.png')
        self.driver.save_screenshot(imgPath)
        logger_cls.info(u'截图:{0}{1}.png'.format(str(timestrmap), name))
Esempio n. 5
0
class Browser:
    __AWAIT_TIMEOUT = 60
    __browser = None
    __browser_kwargs = {}
    __config = {}
    __refs_num = 0

    def get_browser(self):
        return self.__browser

    def __init__(self, headless=True, config={}, **kwargs):
        self.__config = config
        self.__config['SELENIUM_HEADLESS'] = headless
        self.__browser_kwargs = kwargs
        if self.__config.get('SELENIUM_URL'):
            self.__create_browser_session()
        else:
            self.__create_browser_instanse()

    def __del__(self):
        if self.__browser:
            self.__browser.quit()
            logging.debug("%s: Quitted browser at __del__()",
                          self.__config['CELERY_TASK_DEFAULT_QUEUE'])
            del self.__browser

    def __create_browser_instanse(self):
        options = Options()
        if self.__config.get('SELENIUM_HEADLESS'):
            options.headless = True
        if self.__config.get('SELENIUM_BROWSER'):
            options.add_experimental_option("debuggerAddress",
                                            self.__config['SELENIUM_BROWSER'])
        if self.__config.get('SELENIUM_DRIVER'):
            self.__browser_kwargs['executable_path'] = self.__config[
                'SELENIUM_DRIVER']
        if self.__config.get(
                'LOG_LEVEL') and self.__config['LOG_LEVEL'] == logging.DEBUG:
            if self.__config.get('SELENIUM_LOG_PATH'):
                self.__browser_kwargs['service_log_path'] = self.__config[
                    'SELENIUM_LOG_PATH']
            self.__browser_kwargs['service_args'] = ['--verbose']

        if not self.__browser_kwargs.get('executable_path'):
            self.__browser_kwargs['executable_path'] = '/usr/bin/chromedriver'
        self.__browser = Chrome(options=options, **self.__browser_kwargs)
        logging.debug("%s: Created browser instance",
                      self.__config['CELERY_TASK_DEFAULT_QUEUE'])

    def __create_browser_session(self):
        options = Options()
        if self.__config.get('SELENIUM_HEADLESS'):
            options.headless = True
        self.__browser = Remote(command_executor=self.__config['SELENIUM_URL'],
                                options=options)
        logging.debug("Connected to remote browser")

    def __get_by(self, criterium, value):
        ignored_exceptions = (
            NoSuchElementException,
            StaleElementReferenceException,
        )
        try:
            return WebDriverWait(self.__browser,
                                 self.__AWAIT_TIMEOUT,
                                 ignored_exceptions=ignored_exceptions).until(
                                     EC.presence_of_element_located(
                                         (criterium, value)))
        except UnexpectedAlertPresentException as ex:
            raise ex
        except TimeoutException as ex:
            raise NoSuchElementException(
                f"No element with {criterium} {value} was found", ex)

    def click_by_id(self, element_id):
        self.__browser.execute_script(f'$("#{element_id}").click()')

    def doubleclick(self, element):
        ActionChains(self.__browser).double_click(element).perform()

    def execute_script(self, script, *args):
        return self.__browser.execute_script(script, *args)

    def dismiss_alert(self):
        try:
            self.__browser.switch_to_alert().dismiss()
        except NoAlertPresentException:
            pass

    def find_element_by_xpath(self, xpath):
        return self.__browser.find_element_by_xpath(xpath)

    def find_elements_by_xpath(self, xpath):
        return self.__browser.find_elements_by_xpath(xpath)

    def find_elements_by_css_selector(self, css):
        return self.__browser.find_elements_by_css_selector(css)

    def get(self, url):
        exception = None
        for attempt in range(3):
            try:
                self.__browser.get(url)
                exception = None
                break
            except Exception as ex:
                self.quit()
                self.__create_browser_instanse()
                exception = ex
        if exception:
            raise exception

    def get_alert(self):
        try:
            alert = self.__browser.switch_to_alert()
            text = alert.text
            alert.dismiss()
            return text
        except NoAlertPresentException:
            return None

    def get_element_by_class(self, class_name):
        return self.__get_by(By.CLASS_NAME, class_name)

    def get_element_by_css(self, css):
        return self.__get_by(By.CSS_SELECTOR, css)

    def get_element_by_id(self, id):
        return self.__get_by(By.ID, id)

    def get_element_by_name(self, name):
        return self.__get_by(By.NAME, name)

    def switch_to_alert(self):
        return self.__browser.switch_to.alert

    def wait_for_url(self, url):
        try:
            WebDriverWait(self.__browser, 20).until(EC.url_to_be(url))
        except UnexpectedAlertPresentException as ex:
            raise UnexpectedAlertPresentException(ex.alert_text,
                                                  f"Didn't get URL {url}")
        except Exception as ex:
            raise Exception(f"Didn't get URL {url}", ex)

    def close(self):
        self.__browser.get('about:blank')

    def quit(self):
        if self.__browser:
            self.__browser.quit()
            logging.debug("%s: Quitted browser at quit()",
                          self.__config['CELERY_TASK_DEFAULT_QUEUE'])
            del self.__browser

    @property
    def title(self):
        return self.__browser.title