Пример #1
0
    def __createdriver(self, headless=True):
        """
        Tworzenie webdriver-a
        :param headless: Boolean
        :return: Driver object
        """
        print("Create driver")

        firefox_options = FirefoxOptions()
        try:
            if headless:
                # firefox_options.headless = True
                # firefox_options.add_argument('--headless')
                # firefox_options.add_argument('--no-sandbox')
                # firefox_options.add_argument('--disable-dev-shm-usage')

                driver = RemoteWebDriver(
                    options=firefox_options,
                    command_executor="http://geckodriver:4444")
            else:
                driver = RemoteWebDriver(
                    options=firefox_options,
                    command_executor="http://geckodriver:4444")
        except Exception as e:
            print(str(e))
            return

        # Set timeout
        # driver.set_page_load_timeout(30)

        return driver
Пример #2
0
    def mode_remote(self):
        """Open new brower on remote mode

        Raises:
            CoreException -- browser name is not in valid values list
        """
        browser_name = self.bot_config.config['browser']
        url_hub = self.bot_config.config['url_hub']
        self.log.debug('Starting browser with mode : REMOTE ...')
        if browser_name == 'firefox':
            self.curr_caps = DesiredCapabilities.FIREFOX.copy()

        elif browser_name == 'chrome':
            self.curr_caps = DesiredCapabilities.CHROME.copy()

        elif browser_name == 'iexplorer':
            self.curr_caps = DesiredCapabilities.INTERNETEXPLORER.copy()

        elif browser_name == 'phantomjs':
            self.curr_caps = DesiredCapabilities.PHANTOMJS.copy()

        elif browser_name == 'edge':
            self.curr_caps = DesiredCapabilities.EDGE.copy()

        elif browser_name == 'opera':
            self.curr_caps = DesiredCapabilities.OPERA.copy()
        else:
            raise CoreException(message='Bad browser selected')

        self.curr_driver = RemoteWebDriver(command_executor=url_hub,
                                           desired_capabilities=self.curr_caps)
        self.log.info('Started browser with mode : REMOTE OK')
Пример #3
0
 def create_driver(self) -> RemoteWebDriver:
     desired_capabilities = self.config.browser_capabilities
     return RemoteWebDriver(
         command_executor=self.config.full_grid_endpoint,
         desired_capabilities=desired_capabilities,
         browser_profile=None,
         proxy=None,
         keep_alive=False,
         file_detector=None,
         options=None,
     )
Пример #4
0
 def setUp(self):
     """ Start a new browser instance for each test """
     self._screenshot_number = 1
     self.browser = os.getenv('SELENIUM_BROWSER',
                              settings.SELENIUM_DEFAULT_BROWSER)
     if os.getenv('SELENIUM_HOST'):
         self.sel = self.sauce_labs_driver()
     elif self.browser == 'firefox':
         self.sel = Firefox(self.get_firefox_profile())
     elif self.browser == 'htmlunit':
         self.sel = RemoteWebDriver(
             desired_capabilities=DesiredCapabilities.HTMLUNITWITHJS)
     elif self.browser in ['ios', 'ipad', 'ipod', 'iphone']:
         capabilities = {
             'app': 'safari',
             'browserName': '',
             'device': 'iPhone Simulator',
             'os': 'iOS 6.1'
         }
         self.sel = RemoteWebDriver(
             command_executor=self.appium_command_executor(),
             desired_capabilities=capabilities)
     elif self.browser == 'opera':
         self.sel = RemoteWebDriver(
             desired_capabilities=DesiredCapabilities.OPERA)
     elif self.browser == 'iexplore':
         self.sel = RemoteWebDriver(
             desired_capabilities=DesiredCapabilities.INTERNETEXPLORER)
     elif self.browser == 'phantomjs':
         self.sel = PhantomJS(
             service_args=['--debug=true', '--webdriver-loglevel=DEBUG'])
     elif self.browser == 'safari':
         # requires a Safari extension to be built from source and installed
         self.sel = RemoteWebDriver(
             desired_capabilities=DesiredCapabilities.SAFARI)
     else:
         self.sel = Chrome()
     self.sel.set_page_load_timeout(settings.SELENIUM_PAGE_LOAD_TIMEOUT)
     # Give the browser a little time; Firefox throws random errors if you
     # hit it too soon
     time.sleep(1)
Пример #5
0
    def mode_remote(self, browser_name='chrome'):
        """Open new brower on remote mode

        Raises:
            CoreException -- browser name is not in valid values list
        """
        url_hub = self.settings.get('url_hub')
        self.log.debug('Starting browser with mode : REMOTE ...')
        self.curr_driver = RemoteWebDriver(command_executor=url_hub,
                                           desired_capabilities=self.curr_caps,
                                           options=self.curr_options)
        self.log.info('Started browser with mode : REMOTE OK')
Пример #6
0
    def __init__(self, **kwargs):
        chrome_options = Options()
        chrome_options.add_argument("--headless")

        # 对应的chromedriver的放置目录
        # driver = webdriver.Chrome(executable_path=('/Applications/Google\ Chrome.app/Contents/MacOS/chromedriver'), chrome_options=chrome_options)
        # 可以采用remote方式,连接单独启动的chromedriver
        self.browser = RemoteWebDriver(settings.REMOTE_HEADLESS_CHROME,
                                       options=chrome_options)

        super().__init__(**kwargs)
        dispatcher.connect(
            self.spider_closed, signals.spider_closed
        )  # dispatcher.connect()信号分发器,第一个参数信号触发函数,第二个参数是触发信号,signals.spider_closed是爬虫结束信号
Пример #7
0
 def setup_jasmine(self):
     browser = config.browser
     if browser == "firefox":
         self.profile = webdriver.firefox.firefox_profile.FirefoxProfile()
         self.profile.native_events_enabled
         self.driver = webdriver.Firefox(self.profile)
         self.profile_path = self.profile.path 
     if browser =="chrome":
         if config.use_remote_webdriver:
             self.driver = RemoteWebDriver(config.remote_webdriver_url, DesiredCapabilities.CHROME);
         else:
             self.driver = webdriver.Chrome(config.chrome_driver_filename)   
     self.base_url =config.jasmine_url 
     self.driver.get(self.base_url)  
     self.verificationErrors = []
Пример #8
0
 def setup(self):
     browser = config.browser
     if browser == "firefox":
         self.profile = webdriver.firefox.firefox_profile.FirefoxProfile()
         self.profile.native_events_enabled
         self.driver = webdriver.Firefox(self.profile)
         self.profile_path = self.profile.path 
     if browser =="chrome":
         if config.use_remote_webdriver:
             self.driver = RemoteWebDriver(config.remote_webdriver_url, DesiredCapabilities.CHROME);
         else:
             self.driver = webdriver.Chrome(config.chrome_driver_filename)   
     self.base_url =config.url 
     self.driver.get(self.base_url)  
     self.verificationErrors = []
     print "Starting " + self.testname + " at "+strftime("%Y_%m_%d__%H_%M_%S")
Пример #9
0
def start_browser(browser, executable_path, html_out):
  if browser == 'chrome' or browser == 'dartium':
    # Note: you need ChromeDriver *in your path* to run Chrome, in addition to
    # installing Chrome. Also note that the build bot runs have a different path
    # from a normal user -- check the build logs.
    options = selenium.webdriver.chrome.options.Options()
    if browser == 'dartium':
      script_dir = os.path.dirname(os.path.abspath(__file__))
      dartium_dir = os.path.join(script_dir, '..', '..', 'client', 'tests',
                                 'dartium')
      # enable ShadowDOM and style scoped for Dartium
      options.add_argument('--enable-shadow-dom')
      options.add_argument('--enable-style-scoped')
      if executable_path is not None:
        options.binary_location = executable_path
      elif platform.system() == 'Windows':
        options.binary_location = os.path.join(dartium_dir, 'chrome.exe')
      elif platform.system() == 'Darwin':
        options.binary_location = os.path.join(dartium_dir, 'Chromium.app',
                                               'Contents', 'MacOS', 'Chromium')
      else:
        options.binary_location = os.path.join(dartium_dir, 'chrome')
    return selenium.webdriver.Chrome(chrome_options=options)
  elif browser == 'ff':
    script_dir = os.path.dirname(os.path.abspath(__file__))
    profile = selenium.webdriver.firefox.firefox_profile.FirefoxProfile()
    profile.set_preference('dom.max_script_run_time', 0)
    profile.set_preference('dom.max_chrome_script_run_time', 0)
    profile.set_preference('app.update.auto', True)
    profile.set_preference('app.update.enabled', True)
    return selenium.webdriver.Firefox(firefox_profile=profile)
  elif ((browser == 'ie9' or browser == 'ie10') and
      platform.system() == 'Windows'):
    return selenium.webdriver.Ie()
  elif browser == 'safari' and platform.system() == 'Darwin':
    # TODO(efortuna): Ensure our preferences (no pop-up blocking) file is the
    # same (Safari auto-deletes when it has too many "crashes," or in our case,
    # timeouts). Come up with a less hacky way to do this.
    backup_safari_prefs = os.path.dirname(__file__) + '/com.apple.Safari.plist'
    if os.path.exists(backup_safari_prefs):
      shutil.copy(backup_safari_prefs,
           '/Library/Preferences/com.apple.Safari.plist')
    sel = selenium.selenium('localhost', 4444, "*safari", html_out)
    try:
      sel.start()
      return sel
    except socket.error:
      print_server_error()
  elif browser == 'opera':
    try:
      driver = RemoteWebDriver(desired_capabilities=DesiredCapabilities.OPERA)
      # By default, Opera sets their script timeout (the amount of time they
      # expect to hear back from the JavaScript file) to be 10 seconds. We just
      # make it an impossibly large number so that it doesn't time out for this
      # reason, so it behaves like all of the other browser drivers.
      driver.set_script_timeout(9000)
      # If the webpage contains document.onreadystatechanged = function() {...}
      # page load event does not correctly get fired and caught (OperaDriver
      # bug). This is a band-aid.
      driver.set_page_load_timeout(1)
      return driver
    except urllib2.URLError:
      print_server_error()
  else:
    raise Exception('Incompatible browser and platform combination.')
Пример #10
0
import os
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time

chrome_options = Options()
chrome_options.add_argument("--headless")

base_url = "http://www.baidu.com/"
# 对应的chromedriver的放置目录
# driver = webdriver.Chrome(executable_path=('/Applications/Google\ Chrome.app/Contents/MacOS/chromedriver'), chrome_options=chrome_options)
driver = RemoteWebDriver("http://127.0.0.1:9515", options=chrome_options)

driver.get(base_url + "/")

start_time = time.time()
print('this is start_time ', start_time)

driver.find_element_by_id("kw").send_keys("selenium webdriver")
driver.find_element_by_id("su").click()
driver.save_screenshot('screen.png')

driver.close()

end_time = time.time()
print('this is end_time ', end_time)