本文将带您了解关于使用python和selenium连接到phantomJsWebdriver时出现问题的新内容,同时我们还将为您解释pythonselenium点击链接的相关知识,另外,我们还将为您
本文将带您了解关于使用python和selenium连接到phantomJs Webdriver时出现问题的新内容,同时我们还将为您解释python selenium 点击链接的相关知识,另外,我们还将为您提供关于3-Python爬虫-动态HTML/Selenium+PhantomJS/chrome无头浏览器-chromedriver、org.openqa.selenium.phantomjs.PhantomJSDriverService的实例源码、org.openqa.selenium.phantomjs.PhantomJSDriver的实例源码、PhantomJS 2.5.0-beta for Selenium WebDriver不能在Linux中工作的实用信息。
本文目录一览:- 使用python和selenium连接到phantomJs Webdriver时出现问题(python selenium 点击链接)
- 3-Python爬虫-动态HTML/Selenium+PhantomJS/chrome无头浏览器-chromedriver
- org.openqa.selenium.phantomjs.PhantomJSDriverService的实例源码
- org.openqa.selenium.phantomjs.PhantomJSDriver的实例源码
- PhantomJS 2.5.0-beta for Selenium WebDriver不能在Linux中工作
使用python和selenium连接到phantomJs Webdriver时出现问题(python selenium 点击链接)
我试图在使用selenium和phantomjs webdriver的linux服务器上运行python脚本;但是,我不断收到以下错误消息:
selenium.common.exceptions.WebDriverException: Message: Service /home/ubuntu/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs unexpectedly exited. Status code was: 127
这是一个失败并生成此错误的简单测试脚本:
import seleniumfrom selenium import webdriverdriver = webdriver.PhantomJS(executable_path=''/home/ubuntu/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs'')
在路径上调用文件,返回:
file /home/ubuntu/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs/home/ubuntu/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=d0f2a21ff9e0b82113a2095e7cbca7dceaba88fb, stripped
有谁知道如何启动并运行该脚本?我已阅读了似乎类似的stackoverflow问题,并尝试应用建议的解决方案,例如通过npm重新安装幻像并使用sudo执行脚本,但是没有运气。如果我可以提供更多信息,请告诉我。
答案1
小编典典sudo apt-get install libfontconfig
这解决了我的问题。
3-Python爬虫-动态HTML/Selenium+PhantomJS/chrome无头浏览器-chromedriver
动态HTML
爬虫跟反爬虫
动态HTML介绍
- JavaScrapt
- jQuery
- Ajax
- DHTML
- Python采集动态数据
- 从Javascript代码入手采集
- Python第三方库运行JavaScript,直接采集你在浏览器看到的页面
Selenium + PhantomJS
- Selenium: web自动化测试工具
- 自动加载页面
- 获取数据
- 截屏
- 安装: pip install selenium==2.48.0
- 官网: http://selenium-python.readthedocs.io/index.html
- PhantomJS(幽灵)
- 基于Webkit 的无界面的浏览器
- 官网: http://phantomjs.org/download.html
- Selenium 库有有一个WebDriver的API
- WebDriver可以跟页面上的元素进行各种交互,用它可以来进行爬取
- 案例 v36
- chrome + chromedriver
- 下载安装chrome: 下载+安装
- 下载安装chromedriver:
- Selenium操作主要分两大类:
- 得到UI元素
- find_element_by_id
- find_elements_by_name
- find_elements_by_xpath
- find_elements_by_link_text
- find_elements_by_partial_link_text
- find_elements_by_tag_name
- find_elements_by_class_name
- find_elements_by_css_selector
- 基于UI元素操作的模拟
- 单击
- 右键
- 拖拽
- 输入
- 可以通过导入ActionsChains类来做到
- 案例37
- 得到UI元素
案例v36
''''''
通过webdriver操作进行查找1
''''''
from selenium import webdriver
import time
# 通过Keys模拟键盘
from selenium.webdriver.common.keys import Keys
# 操作哪个浏览器就对哪个浏览器建一个实例
# 自动按照环境变量查找相应的浏览器
driver = webdriver.PhantomJS()
# 如果浏览器没有在相应环境变量中,需要指定浏览器位置
driver.get("http://www.baidu.com")
# 通过函数查找title标签
print("Title: {0}".format(driver.title))
案例v37
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
# 可能需要手动添加路径
driver = webdriver.Chrome()
url = "http://www.baidu.com"
driver.get(url)
text = driver.find_element_by_id(''wrapper'').text
print(text)
print(driver.title)
# 得到页面的快照
driver.save_screenshot(''index.png'')
# id="kw" 的是百度的输入框,我们得到输入框的ui元素后直接输入“大熊猫"
driver.find_element_by_id(''kw'').send_keys(u"大熊猫")
# id="su"是百度搜索的按钮,click模拟点击
driver.find_element_by_id(''su'').click()
time.sleep(5)
driver.save_screenshot("daxiongmao.png")
#获取当前页面的cookie
print(driver.get_cookies())
# 模拟输入两个按键 ctrl+ a
driver.find_element_by_id(''kw'').send_keys(Keys.CONTROL, ''a'')
#ctr+x 是剪切快捷键
driver.find_element_by_id(''kw'').send_keys(Keys.CONTROL, ''x'')
driver.find_element_by_id(''kw'').send_keys(u''航空母舰'')
driver.save_screenshot(''hangmu.png'')
driver.find_element_by_id(''su'').send_keys(Keys.RETURN)
time.sleep(5)
driver.save_screenshot(''hangmu2.png'')
# 清空输入框 , clear
driver.find_element_by_id(''kw'').clear()
driver.save_screenshot(''clear.png'')
# 关闭浏览器
driver.quit()
selenium自动化测试 工具:
-用selenium登录的时候,先要get到页面; -get到页面之后就可以准备输入了, -selenium可以模拟输入; -selenium调没有界面的chrome或者PhantomJS -保存快照,手动输入,对静态验证码的一大杀手。
org.openqa.selenium.phantomjs.PhantomJSDriverService的实例源码
protected WebDriver getDriver() { if (this._driver == null) { DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent",spoofUserAgent); caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[] { "--web-security=false","--ssl-protocol=any","--ignore-ssl-errors=true","--webdriver-loglevel=INFO" }); PhantomJSDriverService service = new PhantomJSDriverService.Builder() .usingPort(8080) .usingPhantomJSExecutable(new File("/usr/local/bin/phantomjs")) .build(); this._driver = new PhantomJSDriver(service,caps); } return this._driver; }
public PhantomJSDriverService.Builder createPhantomJsDriverServiceBuilder(SiteConfig siteConfig,DriverConfig driverConfig,File driverFile) { PhantomJSDriverService.Builder serviceBuilder = new PhantomJSDriverService.Builder(); serviceBuilder.usingPhantomJSExecutable(driverFile); // serviceBuilder.usingAnyFreePort(); // serviceBuilder.withEnvironment(Map<String,String>); // serviceBuilder.withLogFile(File); // serviceBuilder.usingPort(int); // // serviceBuilder.withProxy(Proxy); // serviceBuilder.usingGhostDriver(File); // serviceBuilder.usingCommandLineArguments(String[]); // serviceBuilder.usingGhostDriverCommandLineArguments(String[]); return serviceBuilder; }
/** * Generates a phantomJs webdriver. * * @return * A phantomJs webdriver * @throws TechnicalException * if an error occured when Webdriver setExecutable to true. */ private WebDriver generatePhantomJsDriver() throws TechnicalException { final String pathWebdriver = DriverFactory.getPath(Driver.PHANTOMJS); if (!new File(pathWebdriver).setExecutable(true)) { throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE)); } logger.info("Generating Phantomjs driver ({}) ...",pathWebdriver); final DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language","fr-FR"); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,pathWebdriver); capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIoUR,UnexpecteDalertBehavIoUr.ACCEPT); capabilities.setJavascriptEnabled(true); setLoggingLevel(capabilities); // Proxy configuration String proxy = ""; if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) { proxy = Context.getProxy().getHttpProxy(); } capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[] { "--proxy=" + proxy,"--web-security=no","--ssl-protocol=tlsv1","--webdriver-loglevel=NONE" }); return new PhantomJSDriver(capabilities); }
private static WebDriver createPhantomJSDriver(String driverPath) { DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs(); desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,driverPath); desiredCapabilities.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR,true); desiredCapabilities.setCapability(CapabilityType.TAKES_SCREENSHOT,true); desiredCapabilities.setCapability(CapabilityType.ENABLE_PROFILING_CAPABILITY,true); desiredCapabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS,true); desiredCapabilities.setJavascriptEnabled(true); ArrayList<String> cliArgs = new ArrayList<String>(); cliArgs.add("--web-security=true"); cliArgs.add("--ignore-ssl-errors=true"); desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,cliArgs); return new PhantomJSDriver(desiredCapabilities); }
PwnBackWebDriver(PwnBackMediator mediator) { this.mediator = mediator; PhantomJSDriverService driverService = new PhantomJSDriverService.Builder() .usingPhantomJSExecutable(new File(PwnBackSettings.phatomjsLocation)) .build(); DesiredCapabilities capability = new DesiredCapabilities(); capability.setCapability("takesScreenshot",false); String[] args = new String[1]; args[0] = ""; if (checkSSLCertPathDefined()) { args[0] = "--ssl-certificates-path=" + PwnBackSettings.caBundleLocation; } capability.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,args); capability.setCapability("phantomjs.page.settings.userAgent","Mozilla/5.0 (Windows NT 5.1; rv:22.0) Gecko/20100101 Firefox/22.0"); driver = new PhantomJSDriver(driverService,capability); }
@Override public void onTrigger(ProcessContext context,ProcessSession session) { DesiredCapabilities DesireCaps = new DesiredCapabilities(); DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,context.getProperty(DRIVER_LOCATION).getValue()); driver = new PhantomJSDriver(DesireCaps); FlowFile flowFile = session.create(); try { driver.get(url); (new webdriverwait(driver,timeout)).until( ExpectedConditions.visibilityOfElementLocated(getExpectedCondition(selectorType,selector)) ); final byte[] page = formatToXHtml(driver.getPageSource(),StandardCharsets.UTF_8); flowFile = session.write(flowFile,outputStream -> outputStream.write(page)); session.transfer(flowFile,REL_SUCCESS); } catch (Exception e) { flowFile = session.write(flowFile,outputStream -> outputStream.write(e.getMessage().getBytes())); session.transfer(flowFile,REL_FAILURE); } finally { driver.quit(); } session.getProvenanceReporter().create(flowFile); }
public static PhantomJSDriverService createDefaultService(Capabilities desiredCapabilities,Map<String,String> env) { Proxy proxy = null; if (desiredCapabilities != null) { proxy = Proxy.extractFrom(desiredCapabilities); } File phantomjsfile = findPhantomJS(desiredCapabilities,"https://github.com/ariya/phantomjs/wiki","http://phantomjs.org/download.html"); File ghostDriverfile = findGhostDriver(desiredCapabilities,"https://github.com/detro/ghostdriver/blob/master/README.md","https://github.com/detro/ghostdriver/downloads"); Builder builder = new Builder(); builder.usingPhantomJSExecutable(phantomjsfile) .usingGhostDriver(ghostDriverfile) .usingAnyFreePort() .withProxy(proxy) .withLogFile(new File("phantomjsdriver.log")) .usingCommandLineArguments(findCLIArgumentsFromCaps(desiredCapabilities,"phantomjs.cli.args")) .usingGhostDriverCommandLineArguments(findCLIArgumentsFromCaps(desiredCapabilities,"phantomjs.ghostdriver.cli.args")); if(null != env) builder.withEnvironment(env); return builder.build(); }
public PhantomJSDriverService createService(String... arguments) { List<String> argList = new ArrayList<>(); argList.add("--web-security=false"); argList.add("--webdriver-loglevel=TRACE"); argList.add("--load-images=false"); argList.add("--ignore-ssl-errors=true"); argList.add("--ssl-protocol=any"); argList.addAll(Arrays.asList(arguments)); PhantomJSDriverService service = new PhantomJSDriverService.Builder() .usingPhantomJSExecutable(EXECUTABLE) .usingCommandLineArguments(argList.toArray(new String[argList.size()])) .usingAnyFreePort() .build(); LoggingOutputStream loggingOutputStream = new LoggingOutputStream(log,LogLevel.INFO); service.sendOutputTo(loggingOutputStream); return service; }
@Test public void startAndStop() throws IOException { PhantomJSDriverService driverService = factory.createService(); driverService.start(); try { WebDriver webDriver = factory.createWebDriver(driverService); String url = "http://localhost:" + httpServer.getAddress().getPort() + "/webdriverTest"; webDriver.get(url); String currentUrl = webDriver.getcurrenturl(); assertthat(currentUrl).isEqualTo(url); } finally { driverService.stop(); } }
@Ignore @Test public void testProxyHTTP() throws IOException { String[] arguments = factory.createServiceArgumentsBuilder() .httpProxyServer("http://localhost:" + proxyPortProber.getPort()) .build(); PhantomJSDriverService service = factory.createService(arguments); service.start(); try { WebDriver webDriver = factory.createWebDriver(service); webDriver.get("http://www.lgohlke.de"); assertthat(webDriver.getPageSource().length()).isBetween(24000,26000); } finally { service.stop(); } }
@Ignore @Test public void testProxyHTTPS() throws IOException { String[] arguments = factory.createServiceArgumentsBuilder() .httpProxyServer("http://localhost:" + proxyPortProber.getPort()) .build(); PhantomJSDriverService service = factory.createService(arguments); service.start(); try { WebDriver webDriver = factory.createWebDriver(service); webDriver.get("https://www.google.de"); assertthat(webDriver.getPageSource().length()).isBetween(100000,110000); } finally { service.stop(); } }
@Test public void testSeleniumGhostDriver() { File phantomjs = Phanbedder.unpack(); DesiredCapabilities dcaps = new DesiredCapabilities(); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomjs.getAbsolutePath()); PhantomJSDriver driver = new PhantomJSDriver(dcaps); try { driver.get("https://www.google.com"); WebElement query = driver.findElement(By.name("q")); query.sendKeys("Phanbedder"); query.submit(); Assertions.assertthat(driver.getTitle()).contains("Phanbedder"); } finally { driver.quit(); } }
private static DesiredCapabilities initbrowserCapabilities() { DesiredCapabilities browserCapabilities = new DesiredCapabilities(); browserCapabilities.setJavascriptEnabled(true); if (StringUtils.isNotEmpty(PHANTOM_JS_PATH_PROP)) { System.out.printf("\n\nSetting Phantom JS path to %s\n\n%n",PHANTOM_JS_PATH_PROP); browserCapabilities.setCapability( PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,PHANTOM_JS_PATH_PROP); } browserCapabilities.setCapability("takesScreenshot",true); browserCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,buildPhantomJsCommandLineArguments()); browserCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,new String[]{ formatArgument(LOG_LEVEL_ARG,ERROR) }); return browserCapabilities; }
/** * PhantomJS bean * @param capabilities Desirercapabilities for WebDriver * @return WebDriver instance */ @Bean(name="phantomJS") @Lazy(true) @Scope(Configurablebeanfactory.ScopE_PROTOTYPE) public WebDriver phantomJS(DesiredCapabilities capabilities){ DesiredCapabilities phantomJSCap = new DesiredCapabilities(); phantomJSCap.setJavascriptEnabled(true); try { phantomJSCap.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,URLDecoder.decode(getClass() .getClassLoader().getResource(".").getPath(),"UTF-8")+System.getProperty("file.separator")+"downloads"+System.getProperty("file.separator")+"phantomjs-2.0.0-windows"+System.getProperty("file" + ".separator")+"phantomjs.exe"); } catch (UnsupportedEncodingException e) { log.error("Exception occured constructing path to executable: {}",e); } phantomJSCap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[]{ "--ignore-ssl-errors=yes","--web-security=false","--ssl-protocol=any"}); phantomJSCap.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX,"Y"); phantomJSCap.setCapability("phantomjs.page.settings.userAgent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/41.0.2228.0 Safari/537.36"); phantomJSCap.setCapability("elementScrollBehavior",1); return new PhantomJSDriver(capabilities.merge(phantomJSCap)); }
public static void main(String[] args) { //设置相应的驱动程序的位置 System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY,"F:\\selenium\\chromedriver.exe"); System.setProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"F:\\selenium\\phantomjs.exe"); //任务执行线程池大小 int threadCount = 5; SpiderConfig spiderConfig = SpiderConfig.create("baidu",threadCount) .setEmptySleepMillis(1000) .setExitWhenComplete(true); SiteConfig siteConfig = SiteConfig.create() .setMaxConnTotal(200) .setMaxConnPerRoute(100); //定义WebDriver池 WebDriverPool webDriverPool = new WebDriverPool( new WebDriverFactory(),new DefaultWebDriverChooser(DriverType.CHROME),threadCount); //使用WebDriverDownloader请求任务下载器 Downloader downloader = new WebDriverDownloader(webDriverPool); Spider spider = Spider.create(spiderConfig,siteConfig,new BaiduPageProcessor()) .setDownloader(downloader) //设置请求任务下载器 .setPipeline(new BaiduPipeline()) .addStartRequests("https://www.baidu.com/s?wd=https"); //监控 SpiderMonitor.register(spider); //启动 spider.start(); }
public static DesiredCapabilities getDefaultDesiredCapabilities() { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setJavascriptEnabled(true); capabilities.setCapability("acceptSslCerts",true); capabilities.setCapability("takesScreenshot",true); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,asList("--ignore-ssl-errors=true")); LoggingPreferences loggingPreferences = new LoggingPreferences(); loggingPreferences.enable(LogType.broWSER,Level.ALL); capabilities.setCapability(CapabilityType.LOGGING_PREFS,loggingPreferences); return capabilities; }
/** * * @param poolsize * @param loadImg * 是否加载图片,默认不加载 */ public PhantomjsWebDriverPool(int poolsize,boolean loadImg,String phantomjsPath) { this.CAPACITY = poolsize; innerQueue = new LinkedBlockingDeque<WebDriver>(poolsize); PHANTOMJS_PATH = phantomjsPath; caps.setJavascriptEnabled(true); caps.setCapability("webStorageEnabled",true); caps.setCapability( PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,PHANTOMJS_PATH); // caps.setCapability("takesScreenshot",false); caps.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/50.0.2661.102 Safari/537.36"); ArrayList<String> cliArgsCap = new ArrayList<String>(); // http://phantomjs.org/api/command-line.html cliArgsCap.add("--web-security=false"); cliArgsCap.add("--ssl-protocol=any"); cliArgsCap.add("--ignore-ssl-errors=true"); if (loadImg) { cliArgsCap.add("--load-images=true"); } else { cliArgsCap.add("--load-images=false"); } caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,cliArgsCap); caps.setCapability( PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,new String[] { "--logLevel=INFO" }); }
public PhantomJSDriverService getPhantomJsService() { return new PhantomJSDriverService.Builder() .usingAnyFreePort() .usingPhantomJSExecutable( new File(ResourceHelper .getResourcePath("driver/phantomjs.exe"))) .withLogFile( new File(ResourceHelper .getResourcePath("logs/phantomjslogs/") + "phantomjslogs" + DateTimeHelper.getCurrentDateTime() + ".log")) .build(); }
private static DesiredCapabilities getPhantomCapabilities(Locale lang) { DesiredCapabilities capabilities = DesiredCapabilities.phantomjs(); String value = lang.getLanguage().toLowerCase(); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language",value); capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIoUR,UnexpecteDalertBehavIoUr.IGnorE); return capabilities; }
/** * Use phantomjs to fetch the web content. * @return */ protected WebDriver phantomJsDriver() { String phantomJsExecPath = System.getProperty("phantomjsExec"); if(phantomJsExecPath == null || "".equals(phantomJsExecPath)) { phantomJsExecPath = "/usr/local/bin/phantomjs"; } DesiredCapabilities dc = new DesiredCapabilities(); dc.setJavascriptEnabled(true); dc.setCapability( PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomJsExecPath ); return new PhantomJSDriver(dc); }
private static WebDriver getPhantomJsDriver() { final DesiredCapabilities caps = DesiredCapabilities.phantomjs(); caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[] { "--ignore-ssl-errors=true","--web-security=false" }); final PhantomJSDriver phantomJSDriver = new PhantomJSDriver(caps); phantomJSDriver.manage().window().setSize(new Dimension(1280,800)); return phantomJSDriver; }
@BeforeClass public static void setupEnv() throws IOException { String phantomJSPath = System.getProperty("phantomjs.binary"); File phantomJSExec = new File(phantomJSPath); service = new PhantomJSDriverService.Builder().usingPhantomJSExecutable(phantomJSExec).build(); service.start(); }
@BeforeClass public static void setupEnv() throws IOException { String phantomJSPath = System.getProperty("phantomjs.binary"); File phantomJSExec = new File(phantomJSPath); service = new PhantomJSDriverService.Builder().usingPhantomJSExecutable(phantomJSExec).build(); service.start(); }
@BeforeClass public static void setupEnv() throws IOException { String phantomJSPath = System.getProperty("phantomjs.binary"); File phantomJSExec = new File(phantomJSPath); service = new PhantomJSDriverService.Builder().usingPhantomJSExecutable(phantomJSExec).build(); service.start(); }
@BeforeClass public static void setupEnv() throws IOException { String phantomJSPath = System.getProperty("phantomjs.binary"); File phantomJSExec = new File(phantomJSPath); service = new PhantomJSDriverService.Builder().usingPhantomJSExecutable(phantomJSExec).build(); service.start(); }
/** * Setting up PhantoJSDriver * * @return PhantomJSDriver */ public static WebDriver setUpPhantomJSDriver(String driverPath) { File phantomjs = new File(driverPath); DesiredCapabilities dcaps = new DesiredCapabilities(); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[]{"--ignore-ssl-errors=yes","version=2.0","driverVersion=1.2.0"}); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomjs.getAbsolutePath()); return new PhantomJSDriver(dcaps); }
/** * Default implementation throws UnsupportedOperationException * @return WebDriver instance */ protected WebDriver getPhantomJsWebDriver() { String pathToBin = getosspecificBinaryPathFromProp(PHANTOM_JS_BIN_PROP,"phantomjs"); DesiredCapabilities capabilities = getPhantomJsCapabilities(); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,pathToBin); return new PhantomJSDriver(capabilities); }
@Override public void before() { File phantomjs = Phanbedder.unpack(); DesiredCapabilities dcaps = new DesiredCapabilities(); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomjs.getAbsolutePath()); driver = new PhantomJSDriver(dcaps); }
public WebDriver get() { File phantomJsExe = new File("phantomjs-1.9.7-windows/phantomjs.exe"); Map<String,Object> caps = new HashMap<>(); caps.put(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomJsExe.getAbsolutePath()); DesiredCapabilities capabilities = new DesiredCapabilities(caps); WebDriver driver = new PhantomJSDriver(capabilities); Dimension DEFAULT_WINDOW_SIZE = new Dimension(1024,768); driver.manage().window().setSize(DEFAULT_WINDOW_SIZE); return driver; }
@Override public WebDriver getWebDriver() { DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"src/test/resources/phantomjs"); caps.setCapability("takesScreenshot",true); return new PhantomJSDriver(caps); }
private static Capabilities configWebDriver() { DesiredCapabilities dCaps = new DesiredCapabilities(); dCaps.setCapability("takesScreenshot",true); dCaps.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 (KHTML,like Gecko) Chrome/15.0.87" ); dCaps.setCapability( PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"path_to_phantom_js" ); return dCaps; }
private WebDriver createWebDriver() { final DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability("takesScreenshot",true); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,preparePhantomJSExecutable()); final WebDriver driver = new PhantomJSDriver(caps); return driver; }
@Override public DriverService createDriverService() { Builder builder = new PhantomJSDriverService.Builder(); if (driverExecutable != null) builder.usingPhantomJSExecutable(driverExecutable); if (ghostDriver != null) builder.usingGhostDriver(ghostDriver); if (port != null) builder.usingPort(port); if (environment != null) builder.withEnvironment(environment); if (logFile != null) builder.withLogFile(logFile); if (proxy != null) builder.withProxy(new Proxy(proxy)); if (commandLineArguments != null) builder.usingCommandLineArguments(commandLineArguments); if (ghostDriverCommandLineArguments != null) builder.usingGhostDriverCommandLineArguments(ghostDriverCommandLineArguments); return builder.build(); }
/** * * @param config * @return A FirefoxDriver. */ @Override public RemoteWebDriver make(HashMap<String,String> config) { DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); if (System.getProperty(PHANTOMJS_PATH_PROPERTY) != null) { path = System.getProperty(PHANTOMJS_PATH_PROPERTY); } caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,path); return new PhantomJSDriver(caps); }
@Override public PhantomJSDriver initialize() { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[] { "--webdriver-loglevel=NONE","--ignore-ssl-errors=true" }); return new PhantomJSDriver(capabilities); }
public WebDriverEx createPhantomJsWebDriver(SiteConfig siteConfig,DriverConfig driverConfig) throws IOException { File driverFile = createDriverFile(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY); DesiredCapabilities desiredCapabilities = createPhantomJsDesiredCapabilities(siteConfig,driverConfig); PhantomJSDriverService driverService = createPhantomJsDriverService(siteConfig,driverConfig,driverFile); return createWebDriver(driverService,desiredCapabilities,driverConfig); }
public PhantomJSDriverService createPhantomJsDriverService(SiteConfig siteConfig,File driverFile) { return createPhantomJsDriverServiceBuilder(siteConfig,driverFile).build(); }
/** * {@inheritDoc} */ @Override public String getPathproperty() { return PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY; }
public WebDriver getPhantomJsDriver(PhantomJSDriverService sev,Capabilities cap) { return new PhantomJSDriver(sev,cap); }
public WebDriver getPhantomJsDriver(String hubUrl,PhantomJSDriverService sev,Capabilities cap) throws MalformedURLException { return new RemoteWebDriver(new URL(hubUrl),cap); }
org.openqa.selenium.phantomjs.PhantomJSDriver的实例源码
protected WebDriver getDriver() { if (this._driver == null) { DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent",spoofUserAgent); caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[] { "--web-security=false","--ssl-protocol=any","--ignore-ssl-errors=true","--webdriver-loglevel=INFO" }); PhantomJSDriverService service = new PhantomJSDriverService.Builder() .usingPort(8080) .usingPhantomJSExecutable(new File("/usr/local/bin/phantomjs")) .build(); this._driver = new PhantomJSDriver(service,caps); } return this._driver; }
/** * Generates a phantomJs webdriver. * * @return * A phantomJs webdriver * @throws TechnicalException * if an error occured when Webdriver setExecutable to true. */ private WebDriver generatePhantomJsDriver() throws TechnicalException { final String pathWebdriver = DriverFactory.getPath(Driver.PHANTOMJS); if (!new File(pathWebdriver).setExecutable(true)) { throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE)); } logger.info("Generating Phantomjs driver ({}) ...",pathWebdriver); final DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language","fr-FR"); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,pathWebdriver); capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIoUR,UnexpecteDalertBehavIoUr.ACCEPT); capabilities.setJavascriptEnabled(true); setLoggingLevel(capabilities); // Proxy configuration String proxy = ""; if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) { proxy = Context.getProxy().getHttpProxy(); } capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[] { "--proxy=" + proxy,"--web-security=no","--ssl-protocol=tlsv1","--webdriver-loglevel=NONE" }); return new PhantomJSDriver(capabilities); }
public SeleniumExtension() { handlerMap.put(ChromeDriver.class,ChromeDriverHandler.class); handlerMap.put(FirefoxDriver.class,FirefoxDriverHandler.class); handlerMap.put(EdgeDriver.class,EdgeDriverHandler.class); handlerMap.put(OperaDriver.class,OperaDriverHandler.class); handlerMap.put(SafariDriver.class,SafariDriverHandler.class); handlerMap.put(RemoteWebDriver.class,RemoteDriverHandler.class); handlerMap.put(AppiumDriver.class,AppiumDriverHandler.class); handlerMap.put(List.class,ListDriverHandler.class); handlerMap.put(PhantomJSDriver.class,OtherDriverHandler.class); templateHandlerMap.put("chrome",ChromeDriver.class); templateHandlerMap.put("firefox",FirefoxDriver.class); templateHandlerMap.put("edge",EdgeDriver.class); templateHandlerMap.put("opera",OperaDriver.class); templateHandlerMap.put("safari",SafariDriver.class); templateHandlerMap.put("appium",AppiumDriver.class); templateHandlerMap.put("phantomjs",PhantomJSDriver.class); templateHandlerMap.put("chrome-in-docker",RemoteWebDriver.class); templateHandlerMap.put("firefox-in-docker",RemoteWebDriver.class); templateHandlerMap.put("opera-in-docker",RemoteWebDriver.class); }
private static WebDriver createPhantomJSDriver(String driverPath) { DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs(); desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,driverPath); desiredCapabilities.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR,true); desiredCapabilities.setCapability(CapabilityType.TAKES_SCREENSHOT,true); desiredCapabilities.setCapability(CapabilityType.ENABLE_PROFILING_CAPABILITY,true); desiredCapabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS,true); desiredCapabilities.setJavascriptEnabled(true); ArrayList<String> cliArgs = new ArrayList<String>(); cliArgs.add("--web-security=true"); cliArgs.add("--ignore-ssl-errors=true"); desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,cliArgs); return new PhantomJSDriver(desiredCapabilities); }
PwnBackWebDriver(PwnBackMediator mediator) { this.mediator = mediator; PhantomJSDriverService driverService = new PhantomJSDriverService.Builder() .usingPhantomJSExecutable(new File(PwnBackSettings.phatomjsLocation)) .build(); DesiredCapabilities capability = new DesiredCapabilities(); capability.setCapability("takesScreenshot",false); String[] args = new String[1]; args[0] = ""; if (checkSSLCertPathDefined()) { args[0] = "--ssl-certificates-path=" + PwnBackSettings.caBundleLocation; } capability.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,args); capability.setCapability("phantomjs.page.settings.userAgent","Mozilla/5.0 (Windows NT 5.1; rv:22.0) Gecko/20100101 Firefox/22.0"); driver = new PhantomJSDriver(driverService,capability); }
public WebDriver get() throws InterruptedException { WebDriver poll = innerQueue.poll(); if (poll != null) { return poll; } if (refCount.get() < CAPACITY) { synchronized (innerQueue) { if (refCount.get() < CAPACITY) { WebDriver mDriver = new PhantomJSDriver(caps); // 尝试性解决:https://github.com/ariya/phantomjs/issues/11526问题 mDriver.manage().timeouts() .pageLoadTimeout(60,TimeUnit.SECONDS); // mDriver.manage().window().setSize(new Dimension(1366,// 768)); innerQueue.add(mDriver); refCount.incrementAndGet(); } } } return innerQueue.take(); }
@Override public void onTrigger(ProcessContext context,ProcessSession session) { DesiredCapabilities DesireCaps = new DesiredCapabilities(); DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,context.getProperty(DRIVER_LOCATION).getValue()); driver = new PhantomJSDriver(DesireCaps); FlowFile flowFile = session.create(); try { driver.get(url); (new webdriverwait(driver,timeout)).until( ExpectedConditions.visibilityOfElementLocated(getExpectedCondition(selectorType,selector)) ); final byte[] page = formatToXHtml(driver.getPageSource(),StandardCharsets.UTF_8); flowFile = session.write(flowFile,outputStream -> outputStream.write(page)); session.transfer(flowFile,REL_SUCCESS); } catch (Exception e) { flowFile = session.write(flowFile,outputStream -> outputStream.write(e.getMessage().getBytes())); session.transfer(flowFile,REL_FAILURE); } finally { driver.quit(); } session.getProvenanceReporter().create(flowFile); }
@Test public void testSeleniumGhostDriver() { File phantomjs = Phanbedder.unpack(); DesiredCapabilities dcaps = new DesiredCapabilities(); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomjs.getAbsolutePath()); PhantomJSDriver driver = new PhantomJSDriver(dcaps); try { driver.get("https://www.google.com"); WebElement query = driver.findElement(By.name("q")); query.sendKeys("Phanbedder"); query.submit(); Assertions.assertthat(driver.getTitle()).contains("Phanbedder"); } finally { driver.quit(); } }
@Before public void before() throws Exception { final Gauge<Long> gauge = Metrics.newGauge(metricName,new Gauge<Long>() { long current = 42; @Override public Long value() { return current; } }); server = new Server(8081); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setcontextpath("/"); server.setHandler(context); context.addServlet(new ServletHolder(new JimixServlet()),"/jimix/*"); server.start(); // driver = new FirefoxDriver(); driver = new PhantomJSDriver(); driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS); }
/** * PhantomJS bean * @param capabilities Desirercapabilities for WebDriver * @return WebDriver instance */ @Bean(name="phantomJS") @Lazy(true) @Scope(Configurablebeanfactory.ScopE_PROTOTYPE) public WebDriver phantomJS(DesiredCapabilities capabilities){ DesiredCapabilities phantomJSCap = new DesiredCapabilities(); phantomJSCap.setJavascriptEnabled(true); try { phantomJSCap.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,URLDecoder.decode(getClass() .getClassLoader().getResource(".").getPath(),"UTF-8")+System.getProperty("file.separator")+"downloads"+System.getProperty("file.separator")+"phantomjs-2.0.0-windows"+System.getProperty("file" + ".separator")+"phantomjs.exe"); } catch (UnsupportedEncodingException e) { log.error("Exception occured constructing path to executable: {}",e); } phantomJSCap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[]{ "--ignore-ssl-errors=yes","--web-security=false","--ssl-protocol=any"}); phantomJSCap.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX,"Y"); phantomJSCap.setCapability("phantomjs.page.settings.userAgent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/41.0.2228.0 Safari/537.36"); phantomJSCap.setCapability("elementScrollBehavior",1); return new PhantomJSDriver(capabilities.merge(phantomJSCap)); }
public static WebDriver getDriver(String browserType){ if ( StringUtils.isEmpty(browserType) || FIREFOX.equals(browserType)) { return new FirefoxDriver(SeleniumbrowserFactory.getbrowserCapabilities(browserType)); } else if (CHROME.equals(browserType)) { return new ChromeDriver(SeleniumbrowserFactory.getbrowserCapabilities(browserType)); } else if (IE.equals(browserType)) { return new InternetExplorerDriver(SeleniumbrowserFactory.getbrowserCapabilities(browserType)); } else if (PHANTOMJS.equals(browserType)) { return new PhantomJSDriver(); } else if (SAFARI.equals(browserType)) { return new SafariDriver(); } else if (EDGE.equals(browserType)) { return new EdgeDriver(); } else { throw new RuntimeException(String.format("UnkNown browser type: \"%s\"",browserType)); } }
@Before public void setup() throws MalformedURLException { String webAppUrl = System.getProperty(WEBAPP_URL); System.setProperty("SELENIUM_STARTING_URL","http://127.0.0.1:8888/D3Demo.html?gwt.codesvr=127.0.0.1:9997"); // separate localhost and saucelabls/cloudbees env if (System.getProperty(RUN_SELENIUM_LOCALHOST) != null) { // ensure the Firefox with default profile is loaded // in order to use the Firefox instance with GWT dev plugin System.setProperty("webdriver.firefox.profile","default"); driver = new FirefoxDriver(); webAppUrl = "http://127.0.0.1:8888/D3Demo.html?gwt.codesvr=127.0.0.1:9997"; } else { driver = new PhantomJSDriver(); webAppUrl = "http://127.0.0.1:8080/D3Demo.html"; } Dimension size = new Dimension(1920,1080); driver.manage().window().setSize(size); driver.get(webAppUrl); }
@Test public void testWithHeadlessbrowsers(HtmlUnitDriver htmlUnit,PhantomJSDriver phantomjs) { htmlUnit.get("https://bonigarcia.github.io/selenium-jupiter/"); phantomjs.get("https://bonigarcia.github.io/selenium-jupiter/"); assertTrue(htmlUnit.getTitle().contains("JUnit 5 extension")); assertNotNull(phantomjs.getPageSource()); }
private PhantomJSFetcher(String phantomJsBinaryPath,int timeout,boolean loadImages,String userAgent,Collection<Cookie> cookies) { System.setProperty("phantomjs.binary.path",phantomJsBinaryPath); DesiredCapabilities capabilities = DesiredCapabilities.phantomjs(); capabilities.setCapability("phantomjs.page.settings.resourceTimeout",timeout); capabilities.setCapability("phantomjs.page.settings.loadImages",loadImages); capabilities.setCapability("phantomjs.page.settings.userAgent",userAgent); this.webDriver = new PhantomJSDriver(capabilities); this.userAgent = userAgent; this.cookies = cookies; }
private PhantomJSProcessor(String phantomJsBinaryPath,Extractor<WebDriver,?> handler,TaskFilter filter) { System.setProperty("phantomjs.binary.path",userAgent); this.webDriver = new PhantomJSDriver(capabilities); this.handler = handler; this.filter = filter; }
/** * Simulate Enter key */ @Override @PublicAtsApi public void pressEnterKey() { new RealHtmlElementState(this).waitToBecomeExisting(); WebElement element = RealHtmlElementLocator.findElement(this); if (webDriver instanceof PhantomJSDriver) { element.sendKeys(Keys.ENTER); } else { element.sendKeys(Keys.RETURN); } }
private void highlightElement( boolean disregardConfiguration ) { if (webDriver instanceof PhantomJSDriver) { // it is headless browser return; } if (disregardConfiguration || UiEngineConfigurator.getInstance().getHighlightElements()) { try { WebElement webElement = RealHtmlElementLocator.findElement(element); String styleAttrValue = webElement.getAttribute("style"); JavascriptExecutor js = (JavascriptExecutor) webDriver; js.executeScript("arguments[0].setAttribute('style',arguments[1]);",webElement,"background-color: #ff9; border: 1px solid yellow; Box-shadow: 0px 0px 10px #fa0;"); // to change text use: "color: yellow; text-shadow: 0 0 2px #f00;" Thread.sleep(500); js.executeScript("arguments[0].setAttribute('style',styleAttrValue); } catch (Exception e) { // swallow this error as highlighting is not critical } } }
/** * {@inheritDoc} */ @Override public PhantomJSDriver start(Capabilities other) { Capabilities capabilities = this.mergeCapabilities(other); if (capabilities == null) { return new PhantomJSDriver(); } return new PhantomJSDriver(capabilities); }
@Test public void testWithHeadlessbrowsers(HtmlUnitDriver htmlUnit,PhantomJSDriver phantomjs) { htmlUnit.get("https://bonigarcia.github.io/selenium-jupiter/"); phantomjs.get("https://bonigarcia.github.io/selenium-jupiter/"); assertTrue(htmlUnit.getTitle().contains("JUnit 5 extension")); assertNotNull(phantomjs.getPageSource()); }
protected final void loadDriver(boolean quit) { if (quit) { quit(); } driver = new PhantomJSDriver(); driver.manage().window().setSize(new Dimension(1024,768)); driver.manage().timeouts().implicitlyWait(norMAL_WAIT_TIMEOUT,TimeUnit.SECONDS); }
public SeleniumTest(String geckoDriverPath){ //System.setProperty("webdriver.gecko.driver",geckoDriverPath); //driver = new FirefoxDriver(); System.setProperty("phantomjs.binary.path",System.getProperty("user.dir")+"/phantomjs"); driver = new PhantomJSDriver(); driver.manage().window().setSize(new Dimension(1920,1080)); baseUrl = "http://127.0.0.1:8080/#"; driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); wait = new webdriverwait(driver,5); }
/** * Use phantomjs to fetch the web content. * @return */ protected WebDriver phantomJsDriver() { String phantomJsExecPath = System.getProperty("phantomjsExec"); if(phantomJsExecPath == null || "".equals(phantomJsExecPath)) { phantomJsExecPath = "/usr/local/bin/phantomjs"; } DesiredCapabilities dc = new DesiredCapabilities(); dc.setJavascriptEnabled(true); dc.setCapability( PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomJsExecPath ); return new PhantomJSDriver(dc); }
@Override public WebDriver getDefaultDriver() { if(System.getProperty("phantomjs.binary.path") != null) { return new PhantomJSDriver(); } else { return new FirefoxDriver(); } }
private static WebDriver getPhantomJsDriver() { final DesiredCapabilities caps = DesiredCapabilities.phantomjs(); caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[] { "--ignore-ssl-errors=true","--web-security=false" }); final PhantomJSDriver phantomJSDriver = new PhantomJSDriver(caps); phantomJSDriver.manage().window().setSize(new Dimension(1280,800)); return phantomJSDriver; }
public WebDriver getPhantomDriver() { System.out.println("getting phantom driver"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setJavascriptEnabled(true); capabilities.setCapability("webSecurityEnabled",true); PhantomJSDriver driver = new PhantomJSDriver(capabilities); driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICITLY_WAIT_SECS,TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(DEFAULT_PAGE_LOAD_TIMEOUT_SECS,TimeUnit.SECONDS); return driver; }
/** * Setting up PhantoJSDriver * * @return PhantomJSDriver */ public static WebDriver setUpPhantomJSDriver(String driverPath) { File phantomjs = new File(driverPath); DesiredCapabilities dcaps = new DesiredCapabilities(); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,new String[]{"--ignore-ssl-errors=yes","version=2.0","driverVersion=1.2.0"}); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomjs.getAbsolutePath()); return new PhantomJSDriver(dcaps); }
public static void main(String[] args) { WebDriver driver = new PhantomJSDriver(); driver.get("http://www.jan-exner.de/"); JavascriptExecutor js = (JavascriptExecutor) driver; List<Object> dcrlist = (List<Object>) js.executeScript("return _satellite.rules"); for (Iterator<Object> iterator = dcrlist.iterator(); iterator.hasNext();) { Object object = (Object) iterator.next(); Map<String,Object> map = (Map<String,Object>) object; String ruleName = (String) map.get("name"); System.out.println(ruleName); } }
@Override public WebDriver create() throws Exception { // create the web driver LOGGER.log(Level.INFO,"Creating WebDriver"); DesiredCapabilities dcap = DesiredCapabilities.phantomjs(); WebDriver driver = new PhantomJSDriver(dcap); return driver; }
/** * Screenshooter for HTMLUnit. It saves the html source to disk following the * same pattern as the screenshot path. The HTMLUnit session is transfered to * PhantomJs,which takes the screenshot,and is destroyed. The original * driver is not destroyed * * Note: Javascript events,current page changes,etc.. are not saved and are * not captured in the screenshots taken. * * @param path * - where to save the file. This assumes a png file will be * generated * @param baseUrl * - used to transfer the cookies to the phantomjs driver properly. * * @see #getPhantomJsWebDriver() */ public void saveScreenshotForHtmlUnit(String path,String baseUrl) { final WebDriver driver = this.get(); if (!(driver instanceof HtmlUnitDriver)) { LOG.warn("Wrong driver called screenshooter for HTMLUnit driver,default to regular screenshooter"); this.saveScreenshotAs(path); return; } PhantomJSDriver phantomJs = (PhantomJSDriver) getPhantomJsWebDriver(); try { phantomJs.get(baseUrl); String url = driver.getcurrenturl(); LOG.debug("Url: {}",url); for (Cookie cookie : driver.manage().getCookies()) { LOG.debug("Cookie: {}",cookie.toString()); phantomJs.manage().addCookie(cookie); } phantomJs.get(url); // set current thread to phantomjs,and take screenshot in the default way this.set(phantomJs); LOG.debug("HTML Screenshot taken: {}",this.saveScreenshotAs(path)); } finally { // set back original driver for this thread this.set(driver); phantomJs.close(); phantomJs.quit(); } }
/** * Default implementation throws UnsupportedOperationException * @return WebDriver instance */ protected WebDriver getPhantomJsWebDriver() { String pathToBin = getosspecificBinaryPathFromProp(PHANTOM_JS_BIN_PROP,"phantomjs"); DesiredCapabilities capabilities = getPhantomJsCapabilities(); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,pathToBin); return new PhantomJSDriver(capabilities); }
/** * 将带有 chart、map等动态图表的 html转换为 图片 (可以额外配置 cookie的权限控制). * * @param url 目标URL * @param addedCookie 添加 cookie * @return 图片 byte数组 */ @SuppressWarnings(value = {"unchecked"}) @Override public byte[] convert2Image(String url,Cookie addedCookie,Integer width,Integer height) { PhantomJSDriver driver = null; try { driver = HtmlExporterUtils.prepare(url,addedCookie,width,height); return driver == null ? null : driver.getScreenshotAs(OutputType.BYTES); } finally { HtmlExporterUtils.release(driver); } }
/** * 将带有 chart、map等动态图表的 html转换为 图片 (可以额外配置 cookie的权限控制). * * @param url 目标URL * @param addedCookie 添加 cookie * @return */ @SuppressWarnings(value = {"unchecked"}) @Override public String convert2Image(String url,height); return driver == null ? "" : driver.getScreenshotAs(OutputType.BASE64); } finally { HtmlExporterUtils.release(driver); } }
/** * 将带有 chart、map等动态图表的 html转换为 图片 (可以额外配置 cookie的权限控制). * * @param url 目标URL * @param addedCookie 添加 cookie * @return 图片文件 */ @SuppressWarnings(value = {"unchecked"}) @Override public File convert2Image(String url,height); return driver == null ? null : driver.getScreenshotAs(OutputType.FILE); } finally { HtmlExporterUtils.release(driver); } }
/** * 初始化配置 PhantomJS Driver. * * @param url 目标URL * @param addedCookie 添加 cookie * @return 初始化过的 PhantomJS Driver */ public static PhantomJSDriver prepare(String url,Integer height) { // chrome driver maybe not necessary // download from https://sites.google.com/a/chromium.org/chromedriver/downloads // System.setProperty("webdriver.chrome.driver",// DirUtils.RESOURCES_PATH.concat( // PropUtils.getInstance().getProperty("html.exporter.webdriver.chrome.driver"))); DesiredCapabilities phantomCaps = DesiredCapabilities.chrome(); phantomCaps.setJavascriptEnabled(true); phantomCaps.setCapability("phantomjs.page.settings.userAgent",PropUtils.getInstance().getProperty("html.exporter.user.agent")); PhantomJSDriver driver = new PhantomJSDriver(phantomCaps); driver.manage().timeouts().implicitlyWait(Integer.valueOf(PropUtils.getInstance() .getProperty("html.exporter.driver.timeouts.implicitly.seconds")),TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(Integer.valueOf(PropUtils.getInstance() .getProperty("html.exporter.driver.timeouts.page.load.seconds")),TimeUnit.SECONDS); driver.manage().timeouts().setScriptTimeout(Integer.valueOf(PropUtils.getInstance() .getProperty("html.exporter.driver.timeouts.script.seconds")),TimeUnit.SECONDS); if (width == null || height == null) driver.manage().window().maximize(); else driver.manage().window().setSize(new Dimension(width,height)); if (addedCookie != null) driver.manage().addCookie(addedCookie); driver.get(url); // try { // // timeout is not work,so fix it by sleeping thread // Thread.sleep(Integer.valueOf(PropUtils.getInstance() // .getProperty("html.exporter.driver.timeouts.implicitly.seconds")) * 1000); // } catch (InterruptedException e) { // throw new RuntimeException(e); // } return driver; }
/** * Release those resource of phantomjs,include shutdown phantom process. * * @param driver close cannot shutdown,should do it with quit() */ public static void release(PhantomJSDriver driver) { try { if (driver != null) driver.close(); } finally { if (driver != null) driver.quit(); } }
@Override public void before() { File phantomjs = Phanbedder.unpack(); DesiredCapabilities dcaps = new DesiredCapabilities(); dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomjs.getAbsolutePath()); driver = new PhantomJSDriver(dcaps); }
/** * 60 Max * 40 looks stable * @throws InterruptedException */ //@Test public void testLocalPhantomjs() throws InterruptedException { runMultipleThread(60,new WebDriverFactory(){ public WebDriver getDriver() { return new PhantomJSDriver(); } } ); }
public WebDriver get() { File phantomJsExe = new File("phantomjs-1.9.7-windows/phantomjs.exe"); Map<String,Object> caps = new HashMap<>(); caps.put(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomJsExe.getAbsolutePath()); DesiredCapabilities capabilities = new DesiredCapabilities(caps); WebDriver driver = new PhantomJSDriver(capabilities); Dimension DEFAULT_WINDOW_SIZE = new Dimension(1024,768); driver.manage().window().setSize(DEFAULT_WINDOW_SIZE); return driver; }
@Override public WebDriver getWebDriver() { DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"src/test/resources/phantomjs"); caps.setCapability("takesScreenshot",true); return new PhantomJSDriver(caps); }
PhantomJS 2.5.0-beta for Selenium WebDriver不能在Linux中工作
我在我的testing中使用Selenium WebDriver的PhantomJS二进制文件的Linux版本2.5.0-beta(可在这里 ),但它不工作。 例如,这个testing用例在Ubuntu 16.04中的设置失败:
public class PhantomJsTest { protected WebDriver driver; @BeforeClass public static void setupClass() { System.setProperty("phantomjs.binary.path","/path/to/linux-ubuntu-trusty-x86_64/2.5.0/phantomjs"); } @Before public void setuptest() { driver = new PhantomJSDriver(); } @After public void teardown() { if (driver != null) { driver.quit(); } } @Test public void test() { // my test } }
我得到的错误跟踪如下:
Jan 16,2017 12:50:52 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> INFO: executable: /home/boni/.m2/repository/webdriver/phantomjs/linux-ubuntu-trusty-x86_64/2.5.0/phantomjs Jan 16,2017 12:50:52 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> INFO: port: 14863 Jan 16,2017 12:50:52 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> INFO: arguments: [--webdriver=14863,--webdriver-logfile=/home/boni/Documents/dev/other/webdrivermanager/phantomjsdriver.log] Jan 16,2017 12:50:52 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init> INFO: environment: {} /home/boni/.m2/repository/webdriver/phantomjs/linux-ubuntu-trusty-x86_64/2.5.0/phantomjs: error while loading shared libraries: libicui18n.so.52: cannot open shared object file: No such file or directory Jan 16,2017 12:51:13 PM org.openqa.selenium.os.UnixProcess checkForError SEVERE: org.apache.commons.exec.ExecuteException: Process exited with an error: 127 (Exit value: 127)
这是二进制马车吗?
UPDATE
用Java编写脚本?
在Windows的JAVA_HOME中更新系统variables
SIGSTOP和SIGTSTP会损坏JVM吗?
如何打开使用'ip tuntap'创build的tun设备
LDAP绑定Vssearch
我安装了以下依赖项:
sudo apt-get install libicu-dev
…现在我得到这个错误:
INFO: Detected dialect: OSS PhantomJS has crashed. Please read the bug reporting guide at <http://phantomjs.org/bug-reporting.html> and file a bug report. Jan 16,2017 2:39:35 PM org.apache.http.impl.execchain.RetryExec execute INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:11591: The target server Failed to respond Jan 16,2017 2:39:35 PM org.apache.http.impl.execchain.RetryExec execute INFO: retrying request to {}->http://localhost:11591
如何通过Java不断从Linux获取信息,如果目录中有变化?
Tomcat:startup.bat丢失
Oracle Weblogic 12.1.2节点pipe理器状态java.io.IOException
maven-replacer-plugin和windowspath
Java Web Start应用程序在XMonad上显示空的窗口
你有没有安装所有必要的依赖关系?
从2.5测试版公告 :
对于Ubuntu的二进制文件,你需要安装一些依赖项:
PNG
jpeg webp
OpenSSL的
zlib的
fontconfig和freetype
libicu
关于使用python和selenium连接到phantomJs Webdriver时出现问题和python selenium 点击链接的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于3-Python爬虫-动态HTML/Selenium+PhantomJS/chrome无头浏览器-chromedriver、org.openqa.selenium.phantomjs.PhantomJSDriverService的实例源码、org.openqa.selenium.phantomjs.PhantomJSDriver的实例源码、PhantomJS 2.5.0-beta for Selenium WebDriver不能在Linux中工作等相关内容,可以在本站寻找。
本文标签: