Search in sources :

Example 1 with BrowserVersion

use of com.gargoylesoftware.htmlunit.BrowserVersion in project twitter4j by yusuke.

the class OAuthTest method getAccessToken.

private AccessToken getAccessToken(Twitter twitter, String url, RequestToken rt, String screenName, String password, boolean pinRequired) throws TwitterException {
    BrowserVersion browserVersion = BrowserVersion.getDefault();
    browserVersion.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15");
    HtmlUnitDriver driver = new HtmlUnitDriver(browserVersion);
    driver.get(rt.getAuthorizationURL());
    driver.findElement(By.name("session[username_or_email]")).sendKeys(screenName);
    driver.findElement(By.name("session[password]")).sendKeys(password);
    driver.findElement(By.id("allow")).click();
    if (pinRequired) {
        return twitter.getOAuthAccessToken(rt, driver.findElementsByTagName("code").get(0).getText());
    } else {
        String oauthVerifier = driver.getCurrentUrl().replaceFirst(".*&oauth_verifier=([a-zA-Z0-9]+)$", "$1");
        return twitter.getOAuthAccessToken(rt, oauthVerifier);
    }
}
Also used : BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver)

Example 2 with BrowserVersion

use of com.gargoylesoftware.htmlunit.BrowserVersion in project tuerauf-backend-java by dsteinkopf.

the class DashboardTest method setup.

@Before
public void setup() {
    final BrowserVersion browserVersion = BrowserVersion.FIREFOX_24;
    driver = new HtmlUnitDriver(browserVersion);
    ((HtmlUnitDriver) driver).setJavascriptEnabled(true);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
Also used : BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) Before(org.junit.Before)

Example 3 with BrowserVersion

use of com.gargoylesoftware.htmlunit.BrowserVersion in project wechat by dllwh.

the class HtmlUnitHandler method getWebClient.

/**
 * ----------------------------------------------------- Fields start
 */
/**
 * ----------------------------------------------------- Fields end
 */
/**
 * ----------------------------------------------- [私有方法]
 */
/**
 * @方法描述: 模拟特定浏览器
 * @param browser
 * @param proxy
 * @return
 */
private WebClient getWebClient(CrawlParameter crawlPara) {
    /**
     * 模拟一个浏览器,可以选择IE、Chrome、Firefox等等
     */
    WebClient webClient = null;
    BrowserVersion browser = crawlPara.getBrowse();
    if (null == crawlPara.getProxy()) {
        webClient = new WebClient(browser);
    } else {
        // 代理服务器的配置,代理的配置很简单,只需要配置好地址、端口、用户名与密码即可
        ProxyBean proxy = crawlPara.getProxy();
        webClient = new WebClient(browser, proxy.getProxyHost(), proxy.getProxyPort());
    }
    /**
     * 设置webClient的相关参数
     */
    // 启用JavaScript解释器,默认为true(对于某些动态页面,这是必须的)
    webClient.getOptions().setJavaScriptEnabled(crawlPara.isUseJs());
    // 禁用css支持,可避免自动二次请求CSS进行渲染(对于某些动态页面,这是必须的)
    webClient.getOptions().setCssEnabled(false);
    // 启动客户端重定向
    // webClient.getOptions().setRedirectEnabled(true);
    // 忽略ssl认证
    webClient.getOptions().setUseInsecureSSL(true);
    // JavaScript运行错误时,是否抛出异常
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    // JavaScript运行错误时,是否抛出 response 的错误
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    // 设置连接超时时间 ,这里是10S。如果为0,则无限期等待
    webClient.getOptions().setTimeout(10 * 1000);
    // 设置Ajax异步
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.setJavaScriptTimeout(600 * 1000);
    webClient.getOptions().setActiveXNative(false);
    return webClient;
}
Also used : ProxyBean(com.cdeledu.crawler.common.bean.ProxyBean) NicelyResynchronizingAjaxController(com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController) WebClient(com.gargoylesoftware.htmlunit.WebClient) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

Example 4 with BrowserVersion

use of com.gargoylesoftware.htmlunit.BrowserVersion in project wechat by dllwh.

the class SeleniumHandler method getDriver.

/**
 * @方法描述:
 * @属性:
 *      <ul>
 *      <li>getTitle:用于返回当前网页的标题</li>
 *      <li>getCurrentUrl:用于获取当前网页的URL:</li>
 *      <li>getText:用于存储某个元素的文本值.例如链接、纯文本等</li>
 *      <li>isSelected:用于存储复选框或单选框的勾选情况,返回值为true(勾选)或false(未勾选)</li>
 *      <li>getTagName:用于获取元素的标记名称</li>
 *      <li>isEnabled:用于存储input等元素的可编辑状态,如果可以编辑,则返回true, 否则返回false</li>
 *      </ul>
 * @param crawlPara
 * @return
 */
private WebDriver getDriver(CrawlParameter crawlPara) {
    WebDriver driver = null;
    // 模拟浏览器
    BrowserVersion browser = crawlPara.getBrowse();
    if (browser.isFirefox()) {
        // 启动firefox
        driver = getFirefoxDriver();
    } else if (browser.isChrome()) {
        // 启动chrome
        driver = getChromeDriver();
    } else if (browser.isIE()) {
        // 启动IE
        driver = getIEDriver();
    }
    return driver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

Example 5 with BrowserVersion

use of com.gargoylesoftware.htmlunit.BrowserVersion in project saga by timurstrekalov.

the class InstrumentingProxyServerIT method setUp.

@Before
public void setUp() throws Exception {
    final InstanceFieldPerPropertyConfig config = new InstanceFieldPerPropertyConfig();
    proxyServer = new InstrumentingProxyServer(new HtmlUnitBasedScriptInstrumenter(config));
    fileServer = new FileServer(new File(getClass().getResource("/tests").toURI()).getAbsolutePath());
    proxyServerPort = proxyServer.start();
    fileServerPort = fileServer.start();
    final String proxyUrl = "localhost:" + proxyServerPort;
    final Proxy proxy = new Proxy().setProxyType(Proxy.ProxyType.MANUAL).setHttpProxy(proxyUrl).setSslProxy(proxyUrl);
    final DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
    desiredCapabilities.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
    desiredCapabilities.setBrowserName(BrowserType.FIREFOX);
    driver = new HtmlUnitDriver(desiredCapabilities) {

        @Override
        protected WebClient newWebClient(final BrowserVersion version) {
            config.setBrowserVersion(version);
            return WebClientFactory.newInstance(config);
        }
    };
}
Also used : Proxy(org.openqa.selenium.Proxy) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) InstanceFieldPerPropertyConfig(com.github.timurstrekalov.saga.core.cfg.InstanceFieldPerPropertyConfig) InstrumentingProxyServer(com.github.timurstrekalov.saga.core.server.InstrumentingProxyServer) File(java.io.File) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) WebClient(com.gargoylesoftware.htmlunit.WebClient) FileServer(com.github.timurstrekalov.saga.core.FileServer) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) Before(org.junit.Before)

Aggregations

BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)5 HtmlUnitDriver (org.openqa.selenium.htmlunit.HtmlUnitDriver)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 Before (org.junit.Before)2 ProxyBean (com.cdeledu.crawler.common.bean.ProxyBean)1 NicelyResynchronizingAjaxController (com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController)1 FileServer (com.github.timurstrekalov.saga.core.FileServer)1 InstanceFieldPerPropertyConfig (com.github.timurstrekalov.saga.core.cfg.InstanceFieldPerPropertyConfig)1 InstrumentingProxyServer (com.github.timurstrekalov.saga.core.server.InstrumentingProxyServer)1 File (java.io.File)1 Proxy (org.openqa.selenium.Proxy)1 WebDriver (org.openqa.selenium.WebDriver)1 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)1