public DesiredCapabilities createCapabilities(final DriverConfig webDriverConfig) {
    DesiredCapabilities capability = null;
    capability = DesiredCapabilities.htmlUnit();

    if (webDriverConfig.isEnableJavascript()) {
      capability.setJavascriptEnabled(true);
    } else {
      capability.setJavascriptEnabled(false);
    }

    capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    if (webDriverConfig.getBrowserVersion() != null) {
      capability.setVersion(webDriverConfig.getBrowserVersion());
    }

    if (webDriverConfig.getWebPlatform() != null) {
      capability.setPlatform(webDriverConfig.getWebPlatform());
    }

    if (webDriverConfig.getProxyHost() != null) {
      Proxy proxy = webDriverConfig.getProxy();
      capability.setCapability(CapabilityType.PROXY, proxy);
    }

    return capability;
  }
コード例 #2
0
  public static DesiredCapabilities of(Browser browser) {
    if (browser == null) {
      return null;
    }

    DesiredCapabilities caps;

    switch (browser) {
      case chrome:
        caps = DesiredCapabilities.chrome();
        break;

      case ff:
        caps = DesiredCapabilities.firefox();
        //        caps.setCapability(FirefoxDriver.MARIONETTE,
        //
        // Boolean.getBoolean(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE));
        break;

      case htmlunit:
        caps = DesiredCapabilities.htmlUnit();
        caps.setJavascriptEnabled(false);
        break;

      case htmlunit_js:
        caps = DesiredCapabilities.htmlUnit();
        caps.setJavascriptEnabled(true);
        break;

      case ie:
        caps = DesiredCapabilities.internetExplorer();
        break;

      case operablink:
        caps = DesiredCapabilities.operaBlink();
        break;

      case phantomjs:
        caps = DesiredCapabilities.phantomjs();
        break;

      case safari:
        caps = DesiredCapabilities.safari();
        break;

      default:
        throw new RuntimeException("Cannot determine browser config to use");
    }

    String version = System.getProperty("selenium.browser.version");
    if (version != null) {
      caps.setVersion(version);
    }

    caps.setCapability(HAS_NATIVE_EVENTS, Boolean.getBoolean("selenium.browser.native_events"));

    return caps;
  }
コード例 #3
0
  protected WebDriver createInstanceOf(String className) {
    try {
      DesiredCapabilities capabilities = createCommonCapabilities();
      capabilities.setJavascriptEnabled(true);
      capabilities.setCapability(TAKES_SCREENSHOT, true);
      capabilities.setCapability(ACCEPT_SSL_CERTS, true);
      capabilities.setCapability(SUPPORTS_ALERTS, true);
      if (isPhantomjs()) {
        capabilities.setCapability(
            "phantomjs.cli.args", // PhantomJSDriverService.PHANTOMJS_CLI_ARGS ==
                                  // "phantomjs.cli.args"
            new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});
      }

      Class<?> clazz = Class.forName(className);
      if (WebDriverProvider.class.isAssignableFrom(clazz)) {
        return ((WebDriverProvider) clazz.newInstance()).createDriver(capabilities);
      } else {
        Constructor<?> constructor = Class.forName(className).getConstructor(Capabilities.class);
        return (WebDriver) constructor.newInstance(capabilities);
      }
    } catch (InvocationTargetException e) {
      throw runtime(e.getTargetException());
    } catch (Exception invalidClassName) {
      throw new IllegalArgumentException(invalidClassName);
    }
  }
コード例 #4
0
  private WebDriver setRemoteServer(Map<String, String> getsessionConfig) {
    DesiredCapabilities cap = null;
    browser = System.getProperty("browser");

    if (browser == null) browser = getsessionConfig.get("browser").toString();
    if (browser.equalsIgnoreCase("firefox")) {
      cap = DesiredCapabilities.firefox();
    } else if (browser.equalsIgnoreCase("chrome")) {
      cap = DesiredCapabilities.chrome();
    } else if (browser.equalsIgnoreCase("ie")
        || browser.equalsIgnoreCase("internetexplorer")
        || browser.equalsIgnoreCase("InternetExplorer")) {
      cap = DesiredCapabilities.internetExplorer();
    }

    String seleniumhubAddress = System.getProperty("hub");
    if (seleniumhubAddress == null) {
      seleniumhubAddress = getsessionConfig.get("seleniumserverhost1");
    }
    URL serverhost = null;

    try {
      serverhost = new URL(seleniumhubAddress);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    cap.setJavascriptEnabled(true);
    return new RemoteWebDriver(serverhost, cap);
  }
コード例 #5
0
ファイル: BaseTest.java プロジェクト: xjamundx/ghostdriver
  @BeforeClass
  public static void configure() throws IOException {
    // Read config file
    sConfig = new Properties();
    sConfig.load(new FileReader(CONFIG_FILE));

    // Prepare capabilities
    sCaps = new DesiredCapabilities();
    sCaps.setJavascriptEnabled(true);
    sCaps.setCapability("takesScreenshot", false);

    String driver = sConfig.getProperty("driver", DRIVER_PHANTOMJS);

    // Fetch PhantomJS-specific configuration parameters
    if (driver.equals(DRIVER_PHANTOMJS)) {
      // "phantomjs_exec_path"
      if (sConfig.getProperty("phantomjs_exec_path") != null) {
        sCaps.setCapability(
            PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
            sConfig.getProperty("phantomjs_exec_path"));
      } else {
        throw new IOException(
            String.format(
                "Property '%s' not set!",
                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY));
      }
      // "phantomjs_driver_path"
      if (sConfig.getProperty("phantomjs_driver_path") != null) {
        System.out.println("Test will use an external GhostDriver");
        sCaps.setCapability(
            PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY,
            sConfig.getProperty("phantomjs_driver_path"));
      } else {
        System.out.println("Test will use PhantomJS internal GhostDriver");
      }
    }

    // Disable "web-security", enable all possible "ssl-protocols" and "ignore-ssl-errors" for
    // PhantomJSDriver
    sCaps.setCapability(
        PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
        new String[] {"--web-security=false", "--ssl-protocol=any", "--ignore-ssl-errors=true"});

    // Control LogLevel for GhostDriver, via CLI arguments
    sCaps.setCapability(
        PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,
        new String[] {
          "--logLevel="
              + (sConfig.getProperty("phantomjs_driver_loglevel") != null
                  ? sConfig.getProperty("phantomjs_driver_loglevel")
                  : "INFO")
        });
  }
コード例 #6
0
 private DesiredCapabilities getBrowserCapabilities(Browser browser) {
   DesiredCapabilities capabilities = new DesiredCapabilities();
   if (browser.getName().equals("internet explorer")) {
     capabilities.setCapability(
         InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
   } else if (Browser.CHROME == browser) {
     ChromeOptions options = new ChromeOptions();
     options.addArguments(
         Arrays.asList("allow-running-insecure-content", "ignore-certificate-errors"));
     options.addArguments("--start-maximized");
     capabilities.setCapability(ChromeOptions.CAPABILITY, options);
   } else {
     FirefoxProfile profile = new FirefoxProfile();
     if (Browser.MOBILE == browser) {
       profile.setPreference(
           "general.useragent.override",
           "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
       profile.setAcceptUntrustedCertificates(true);
     }
     if (Browser.NOCOOKIE == browser) {
       profile.setPreference("network.cookie.cookieBehavior", 2);
     }
     profile.setEnableNativeEvents(true);
     capabilities.setCapability(FirefoxDriver.PROFILE, profile);
   }
   if (Browser.NOJS == browser) {
     capabilities.setJavascriptEnabled(false);
   } else {
     capabilities.setJavascriptEnabled(true);
   }
   capabilities.setBrowserName(browser.getName());
   if (StringUtils.isNotEmpty(browser.getVersion())) {
     capabilities.setVersion(browser.getVersion());
   }
   capabilities.setPlatform(Platform.ANY);
   capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
   capabilities.setCapability("trustAllSSLCertificates", true);
   return capabilities;
 }
コード例 #7
0
  @Test
  public void basicHTMLUnitDriverCapabilities() {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    // setting this to false does not impact firefox
    capabilities.setJavascriptEnabled(true);

    WebDriver htmlunit = new HtmlUnitDriver(capabilities);

    htmlunit.get("http://www.compendiumdev.co.uk/selenium/basic_html_form.html");

    assertThat(htmlunit.getTitle(), is("HTML Form Elements"));

    htmlunit.quit();
  }
コード例 #8
0
  public static long runTest(int port)
      throws MalformedURLException, IOException, InterruptedException {
    long totalTime;

    // Start the clock
    Date start = new Date();

    // Ask for a JavaScript-enabled browser
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setJavascriptEnabled(true);

    WebDriver driver = new RemoteWebDriver(new URL("http://localhost:" + port), capabilities);
    Capabilities actualCapabilities = ((RemoteWebDriver) driver).getCapabilities();

    // And now use this to visit Google
    System.out.println("Loading 'http://www.google.com'...");
    driver.get("http://www.google.com");
    System.out.println("Loaded. Current URL is: '" + driver.getCurrentUrl() + "'");

    // Find the text input element by its name
    System.out.println("Finding an Element via [name='q']...");
    WebElement element = driver.findElement(By.name("q"));
    System.out.println("Found.");

    // Enter something to search for
    System.out.println("Sending keys 'Cheese!...'");
    element.sendKeys("Cheese!");
    System.out.println("Sent.");

    // Now submit the form. WebDriver will find the form for us from the element
    System.out.println("Submitting Element...");
    element.submit();
    System.out.println("Submitted.");

    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());
    driver.close();

    totalTime = new Date().getTime() - start.getTime();
    System.out.println("Time elapsed (ms): " + totalTime);

    return totalTime;
  }
コード例 #9
0
  @Test
  public void basicHTMLUnitDriverProxyCapabilities() {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    // setting this to false does not impact firefox
    capabilities.setJavascriptEnabled(true);

    org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    proxy.setHttpProxy(Driver.PROXY).setFtpProxy(Driver.PROXY).setSslProxy(Driver.PROXY);
    capabilities.setCapability(CapabilityType.PROXY, proxy);

    WebDriver htmlunit = new HtmlUnitDriver(capabilities);

    htmlunit.get("http://www.compendiumdev.co.uk/selenium/basic_html_form.html");

    assertThat(htmlunit.getTitle(), is("HTML Form Elements"));

    htmlunit.quit();
  }
コード例 #10
0
  /**
   * @author: Downey Tung
   * @date: Created on 09/03/2015
   * @description: This is test suite for the Daily automation of SMT Daily Automation
   */
  @Before
  public void setUp() throws Exception {
    System.setProperty("phantomjs.binary.path", "/usr/local/Cellar/phantomjs/2.0.0/bin/phantomjs");
    // System.setProperty("phantomjs.binary.path", "/usr/local/bin/phantomjs");
    dCaps = new DesiredCapabilities();
    dCaps.setBrowserName("firefox");
    dCaps.setVersion("39");
    dCaps.setJavascriptEnabled(true);
    dCaps.setCapability("takesScreenshot", true);
    driver = new PhantomJSDriver(dCaps);
    driver.get("https://radioedit.iheart.com");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.switchTo().defaultContent();

    // input on username
    driver.findElement(By.name("username")).sendKeys("svc-resmtrw");
    // input on password
    driver.findElement(By.name("password")).sendKeys("Digital2014");
    // click on Authorize
    driver.findElement(By.xpath("//*[@id='loginForm']/fieldset/div/input")).click();
    Thread.sleep(5000);

    String winHandleBefore = driver.getWindowHandle();
    for (String windhandle : driver.getWindowHandles()) {
      driver.switchTo().window(windhandle);
    }

    // Click on Stream Manager
    driver
        .findElement(By.xpath("//*[@ng-href='http://stream-manager.radioedit.iheart.com']"))
        .click();

    for (String windhandle2 : driver.getWindowHandles()) {
      driver.switchTo().window(windhandle2);
    }
  }
コード例 #11
0
ファイル: Test.java プロジェクト: djh4230/Mo9Crawer
  // li.active>span.next
  public static void main(String[] args) {

    if (args.length < 3) {
      System.out.println("please input two paremeter!");
      System.exit(0);
    }
    String url = args[0];
    String phantomJsPath = args[1];
    String screenShot = args[2];

    DesiredCapabilities cap = DesiredCapabilities.phantomjs();
    cap.setCapability(
        PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
        new String[] {
          "--webdriver-loglevel=DEBUG",
          //			"--proxy=org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
        });
    cap.setCapability(
        PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "zh_CN");
    // cap.setCapability("phantomjs.page.settings.userAgent","Mozilla/5.0 (Macintosh; Intel Mac OS X
    // 10.9; rv:25.0) Gecko/20100101 Firefox/25.0");
    // "C:\\Users\\Administrator\\Desktop\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe"
    cap.setCapability("phantomjs.binary.path", phantomJsPath);
    cap.setJavascriptEnabled(true);
    driver = new PhantomJSDriver(cap);

    WebDriverWait wait = null;
    wait = new WebDriverWait(driver, 60);
    driver.get(url);
    try {
      Thread.currentThread().sleep(5000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    screenShot((TakesScreenshot) driver, screenShot);
  }
コード例 #12
0
ファイル: GitCrawler.java プロジェクト: arcuri82/pg6100
  public static void main(String[] args) {

    // make sure to install in Firefox the plugin from
    // https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
    DesiredCapabilities desiredCapabilities = DesiredCapabilities.htmlUnit();
    desiredCapabilities.setBrowserName("firefox");
    desiredCapabilities.setJavascriptEnabled(true);

    WebDriver driver = new FirefoxDriver(desiredCapabilities);

    int start = 1;
    if (args.length == 1) {
      start = Integer.parseInt(args[0]);
    }

    try {
      crawl(driver, start);
    } catch (Exception e) {
      System.out.println("ERROR: " + e.getMessage());
      e.printStackTrace();
    }

    driver.close();
  }
コード例 #13
0
  public void instantiateBrowser(
      String selectedBrowser,
      String selectedPlatform,
      String applicationURL,
      String applicationContext)
      throws ScreenException, MalformedURLException {

    URL server = new URL("http://localhost:4444/wd/hub/");
    if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_CHROME)) {
      log.info(" LAUNCHING GOOGLECHROME ");
      try {
        capabilities = new DesiredCapabilities();
        capabilities.setBrowserName("chrome");

      } catch (Exception e) {
        e.printStackTrace();
      }

    } else if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_IE)) {
      log.info(" LAUNCHING INTERNET EXPLORE ");
      try {
        capabilities = new DesiredCapabilities();
        capabilities.setJavascriptEnabled(true);
        capabilities.setBrowserName("iexplorer");
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_OPERA)) {
      log.info(" LAUNCHING OPERA ");
      try {

        capabilities = new DesiredCapabilities();
        capabilities.setBrowserName("opera");
        capabilities.setCapability("opera.autostart ", true);
      } catch (Exception e) {
        e.printStackTrace();
      }

    } else if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_SAFARI)) {
      log.info(" LAUNCHING SAFARI ");
      try {

        capabilities = new DesiredCapabilities();
        capabilities.setBrowserName("safari");
        capabilities.setCapability("safari.autostart ", true);
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_FIREFOX)) {
      log.info(" LAUNCHING FIREFOX ");
      capabilities = new DesiredCapabilities();
      capabilities.setBrowserName("firefox");

    } else if (selectedBrowser.equalsIgnoreCase(Constants.BROWSER_HEADLESS)) {
      log.info(" LAUNCHING HTMLUNIT ");
      capabilities = new DesiredCapabilities();
      capabilities.setBrowserName("htmlunit");

    } else if (selectedBrowser.equalsIgnoreCase(Constants.IPHONE_WEBDRIVER)) {
      try {
        log.info(" LAUNCHING IPHONE DRIVER");
        capabilities = new DesiredCapabilities();
        capabilities.setBrowserName("iPhone");
        capabilities.setJavascriptEnabled(true);
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else {
      throw new ScreenException(
          " Only FireFox,InternetExplore Chrome ,Htmlunit and iPhoneWebdriver works");
    }

    /** These 3 steps common for all the browsers */
    if (selectedPlatform.equalsIgnoreCase("WINDOWS")) {
      capabilities.setCapability(CapabilityType.PLATFORM, Platform.WINDOWS);
    } else if (selectedPlatform.equalsIgnoreCase("LINUX")) {
      capabilities.setCapability(CapabilityType.PLATFORM, Platform.LINUX);
    } else if (selectedPlatform.equalsIgnoreCase("MAC")) {
      capabilities.setCapability(CapabilityType.PLATFORM, Platform.MAC);
    }
    driver = new RemoteWebDriver(server, capabilities);
    driver.navigate().to(applicationURL + applicationContext);
  }
コード例 #14
0
  /*
   * Factory method to return a RemoteWebDriver instance given the url of the
   * Grid hub and a Browser instance.
   *
   * @param gridHubUrl : grid hub URI
   *
   * @param browser : Browser object containing info around the browser to hit
   *
   * @param username : username for BASIC authentication on the page to test
   *
   * @param password : password for BASIC authentication on the page to test
   *
   * @return RemoteWebDriver
   */
  public static WebDriver getInstance(
      String gridHubUrl, Browser browser, String username, String password) {

    WebDriver webDriver = null;

    DesiredCapabilities capability = new DesiredCapabilities();
    String browserName = browser.getName();
    capability.setJavascriptEnabled(true);

    // In case there is no Hub
    if (gridHubUrl == null || gridHubUrl.length() == 0) {
      return getInstance(browserName, username, password);
    }

    if (CHROME.equals(browserName)) {
      capability = DesiredCapabilities.chrome();
    } else if (FIREFOX.equals(browserName)) {
      capability = DesiredCapabilities.firefox();

      FirefoxProfile ffProfile = new FirefoxProfile();

      // Authenication Hack for Firefox
      if (username != null && password != null) {
        ffProfile.setPreference("network.http.phishy-userpass-length", 255);
        capability.setCapability(FirefoxDriver.PROFILE, ffProfile);
      }

      capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
    } else if (INTERNET_EXPLORER.equals(browserName)) {

      capability = DesiredCapabilities.internetExplorer();
      capability.setCapability(
          InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    } else if (SAFARI.equals(browserName)) {
      capability = DesiredCapabilities.safari();
    } else if (ANDROID.equals(browserName)) {
      capability = DesiredCapabilities.android();
    } else if (IPHONE.equals(browserName)) {
      capability = DesiredCapabilities.iphone();
    } else {

      capability = DesiredCapabilities.htmlUnit();
      // HTMLunit Check
      if (username != null && password != null) {
        webDriver = (HtmlUnitDriver) AuthenticatedHtmlUnitDriver.create(username, password);
      } else {
        webDriver = new HtmlUnitDriver(true);
      }

      return webDriver;
    }

    capability = setVersionAndPlatform(capability, browser.getVersion(), browser.getPlatform());

    // Create Remote WebDriver
    try {
      webDriver = new RemoteWebDriver(new URL(gridHubUrl), capability);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }

    return webDriver;
  }
コード例 #15
0
  // li.active>span.next
  public static void main(String[] args) {

    if (args.length < 2) {
      System.out.println("please input two paremeter!");
      System.exit(0);
    }
    String filePath = args[0];
    String phantomJsPath = args[1];

    DesiredCapabilities cap = DesiredCapabilities.phantomjs();
    cap.setCapability(
        PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
        new String[] {
          //                       "--webdriver-loglevel=DEBUG",
          //			"--proxy=org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
        });
    cap.setCapability(
        PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "zh_CN");
    // cap.setCapability("phantomjs.page.settings.userAgent","Mozilla/5.0 (Macintosh; Intel Mac OS X
    // 10.9; rv:25.0) Gecko/20100101 Firefox/25.0");
    // "C:\\Users\\Administrator\\Desktop\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe"
    cap.setCapability("phantomjs.binary.path", phantomJsPath);
    cap.setJavascriptEnabled(true);
    driver = new PhantomJSDriver(cap);

    WebDriverWait wait = null;
    wait = new WebDriverWait(driver, 60);
    driver.get(URL);
    /*try {
        Thread.currentThread().sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }*/
    // screenShot((TakesScreenshot) driver, "/home/admin/"+"mo9"+c+".png");
    List<WebElement> nextElement = null;
    ArrayList<String> credits = new ArrayList<String>();

    int prePage = 1;
    int curPage = c;
    BufferedWriter br = null;
    File f = new File(filePath);
    try {
      br = new BufferedWriter(new FileWriter(f, true));
      br.write("mo9账号,真实姓名,身份证号,欠款金额,逾期天数");
      br.write("\r\n");
    } catch (IOException e1) {
      e1.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

    while (!isLast) {
      List<WebElement> elements = null;
      try {
        elements =
            wait.until(
                ExpectedConditions.visibilityOfAllElementsLocatedBy(
                    By.cssSelector("div.faith-list>dl.fs12")));

      } catch (Exception e) {
        e.printStackTrace();
        driver.close();
        driver.quit();
        while (elements == null) {
          try {

            Thread.currentThread().sleep(720000);
            // c=((curPage/15)*15)+1;
            // curPage=c;
            driver = new PhantomJSDriver(cap);
            // driver=new FirefoxDriver(binary,profile);

            wait = new WebDriverWait(driver, 60);
            driver.get("https://www.mo9.com/creditCenter/p/" + curPage);
            elements =
                wait.until(
                    ExpectedConditions.visibilityOfAllElementsLocatedBy(
                        By.cssSelector("div.faith-list>dl.fs12")));
          } catch (Exception e1) {
            e1.printStackTrace();
          }
        }
      }

      System.out.println(curPage);
      for (WebElement element : elements) {
        StringBuffer sb = new StringBuffer();
        String acount =
            element.findElement(By.cssSelector("div.faith-list>dl.fs12>dt.row01")).getText();
        String name =
            element.findElement(By.cssSelector("div.faith-list>dl.fs12>dd.row02")).getText();
        String certiCard =
            element.findElement(By.cssSelector("div.faith-list>dl.fs12>dd.row03")).getText();
        String debt =
            element.findElement(By.cssSelector("div.faith-list>dl.fs12>dd.row04")).getText();
        String overDue =
            element.findElement(By.cssSelector("div.faith-list>dl.fs12>dd.row05")).getText();
        sb.append(acount + "," + name + "," + certiCard + "," + debt + "," + overDue);
        credits.add(sb.toString());
      }
      nextElement = driver.findElements(By.cssSelector("li>a.next"));

      if (credits.size() % 150 == 0 || nextElement.size() == 0) {
        try {
          br = new BufferedWriter(new FileWriter(f, true));
          for (String str : credits) {
            br.write(str);
            br.write("\r\n");
          }
          credits.clear();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          if (br != null) {
            try {
              br.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
        try {
          // Random r=new Random(100);
          // int n=r.nextInt(90);
          Thread.currentThread().sleep(20000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      if (nextElement.size() == 0) {
        isLast = true;
      } else {
        nextElement.get(0).click();
        curPage++;
        try {
          Thread.currentThread().sleep(2000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
コード例 #16
0
  public static DesiredCapabilities of(Browser browser) {
    if (browser == null) {
      return null;
    }

    DesiredCapabilities caps;

    switch (browser) {
      case android:
      case android_real_phone:
        caps = DesiredCapabilities.android();
        break;

      case chrome:
        caps = DesiredCapabilities.chrome();
        break;

      case ff:
        caps = DesiredCapabilities.firefox();
        break;

      case htmlunit:
        caps = DesiredCapabilities.htmlUnit();
        caps.setJavascriptEnabled(false);
        break;

      case htmlunit_js:
        caps = DesiredCapabilities.htmlUnit();
        caps.setJavascriptEnabled(true);
        break;

      case ie:
        caps = DesiredCapabilities.internetExplorer();
        break;

      case opera:
        OperaProfile profile = new OperaProfile();
        profile.preferences().set("Geolocation", "Enable geolocation", true);
        // This pref allows all sites to access geolocation without prompting.
        // A 0 value would deny all sites and -1 would prompt for all sites.
        profile.preferences().set("User Prefs", "Geolocation site state", 1);

        caps = DesiredCapabilities.opera();
        caps.setCapability("opera.profile", profile);
        break;

      case opera_mobile:
        caps = DesiredCapabilities.opera();
        caps.setCapability(OperaSettings.Capability.PRODUCT.getCapability(), OperaProduct.MOBILE);
        break;

      case phantomjs:
        caps = DesiredCapabilities.phantomjs();
        break;

      case safari:
        caps = DesiredCapabilities.safari();
        break;

      case ipad:
        caps = DesiredCapabilities.ipad();
        break;

      case iphone:
        caps = DesiredCapabilities.iphone();
        break;

      default:
        throw new RuntimeException("Cannot determine browser config to use");
    }

    String version = System.getProperty("selenium.browser.version");
    if (version != null) {
      caps.setVersion(version);
    }

    caps.setCapability(HAS_NATIVE_EVENTS, Boolean.getBoolean("selenium.browser.native_events"));

    return caps;
  }
コード例 #17
0
 private static WebDriver getChromeDriver(String path) {
   // TODO Auto-generated method stub
   System.setProperty("webdriver.chrome.driver", path);
   capabilities.setJavascriptEnabled(true);
   return new ChromeDriver(capabilities);
 }