// chrome public void seleniumChrome(String filePath, String url) throws InterruptedException { System.out.println(String.format("Fetching %s...", url)); // 设置 System.setProperty( "webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); System.out.println("chromedriver opened"); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized")); WebDriver driver = new org.openqa.selenium.chrome.ChromeDriver(capabilities); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); // 5秒内没打开,重新加载 while (true) { try { driver.get(url); } catch (Exception e) { driver.quit(); driver = new ChromeDriver(); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); continue; } break; } // save page String html = driver.getPageSource(); saveHtml(filePath, html); System.out.println("save finish..."); // Close the browser Thread.sleep(2000); driver.quit(); }
/** * 实现功能:爬取作者页面,输入是 “页面保存路径filepath” 和 “作者页面链接url” 10秒内没打开页面会重开 无需下来页面,所需信息都在页面顶部 使用技术:Selenium * * @param filePath, url */ public void seleniumCrawlerAuthor(String filePath, String url) throws InterruptedException { System.out.println(String.format("Fetching %s...", url)); // 设置 // System.setProperty("webdriver.ie.driver","C:\\Program Files\\Internet // Explorer\\IEDriverServer.exe"); // System.out.println("InternetExplorerDriver opened"); WebDriver driver = new InternetExplorerDriver(); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); // 5秒内没打开,重新加载 while (true) { try { driver.get(url); } catch (Exception e) { driver.quit(); driver = new InternetExplorerDriver(); driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS); continue; } break; } // save page String html = driver.getPageSource(); saveHtml(filePath, html); System.out.println("save finish..."); // Close the browser Thread.sleep(2000); driver.quit(); }
/** * 实现功能:爬取主题页面,输入是 “页面保存路径filepath” 和 “主题页面链接url” 下拉四次主题页面,10秒内没打开页面会重开(问题数目为49-59) 调整 下拉次数 * 可以得到问题数目会 增加 使用技术:Selenium * * @param filePath, url * @throws InterruptedException */ public static void seleniumCrawlerSubject(String filePath, String url) throws InterruptedException { // 设置 // System.setProperty("webdriver.ie.driver","C:\\Program Files\\Internet // Explorer\\IEDriverServer.exe"); // System.out.println("InternetExplorerDriver opened"); File file = new File(filePath); int delay = 12; if (!file.exists()) { // try{ // // } catch(Exception e) { // // } WebDriver driver = new InternetExplorerDriver(); // 5秒内没打开,重新加载 driver.manage().timeouts().pageLoadTimeout(delay, TimeUnit.SECONDS); while (true) { try { driver.get(url); } catch (Exception e) { driver.quit(); driver = new InternetExplorerDriver(); driver.manage().timeouts().pageLoadTimeout(delay, TimeUnit.SECONDS); continue; } break; } System.out.println("Page title is: " + driver.getTitle()); // roll the page JavascriptExecutor JS = (JavascriptExecutor) driver; try { JS.executeScript("scrollTo(0, 5000)"); System.out.println("1"); Thread.sleep(5000); // 调整休眠时间可以获取更多的内容 JS.executeScript("scrollTo(5000, 10000)"); System.out.println("2"); Thread.sleep(5000); JS.executeScript("scrollTo(10000, 30000)"); // document.body.scrollHeight System.out.println("3"); Thread.sleep(5000); JS.executeScript("scrollTo(10000, 80000)"); // document.body.scrollHeight System.out.println("4"); Thread.sleep(5000); } catch (Exception e) { System.out.println("Error at loading the page ..."); driver.quit(); } // save page String html = driver.getPageSource(); saveHtml(filePath, html); System.out.println("save finish"); // Close the browser Thread.sleep(2000); driver.quit(); } else { System.out.println(filePath + "已经存在,不必再次爬取..."); } }
@Override public void run() { if (page.url() == null) { logger.error("No url specified!"); driver.quit(); return; } driver.get(page.url()); filter.fillFilterForm(); inspector.inspect(); driver.quit(); }
@Test public void canStartMultipleIeDriverInstances() { WebDriver firstDriver = newIeDriver(); WebDriver secondDriver = newIeDriver(); try { firstDriver.get(pages.xhtmlTestPage); secondDriver.get(pages.formPage); assertEquals("XHTML Test Page", firstDriver.getTitle()); assertEquals("We Leave From Here", secondDriver.getTitle()); } finally { firstDriver.quit(); secondDriver.quit(); } }
public void Test1() { WebDriver dr = new FirefoxDriver(); dr.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS); try { dr.get("http://www.w3schools.com/xsl/xsl_functions.asp"); } catch (Exception e) { // ignore page load time out // e.printStackTrace(); } System.out.println(dr.getCurrentUrl()); WebDriverWait wait = new WebDriverWait(dr, 10); wait.until( ExpectedConditions.visibilityOfElementLocated( By.xpath("//h3[contains(.,'Accessor Functions')]"))); System.out.println("page loaded"); WebElement eTable = dr.findElement( By.xpath("//h3[contains(.,'Accessor Functions')]/following-sibling::table[1]")); Table tTable = new Table(eTable); System.out.println(tTable.getCellText(1, 1)); // row 1, column 1 System.out.println(tTable.getCellText("fn,nilled", 1)); // row 2, column 1 System.out.println(tTable.getCellText(1, "Name")); // row 1, column 1 System.out.println(tTable.getCellText("fn,nilled", "Description")); // row 2, column 2 dr.quit(); }
@Test public void checkTakeawayGuestRegistration() throws AWTException, InterruptedException { WebDriver driver = browserFactory.startBrowser( "firefox", "https://systest.harvester.172.26.161.242.xip.io/restaurants/eastandwestmidlands/thewychwayinndroitwich/takeaway"); TakeAwayPage takeaway = PageFactory.initElements(driver, TakeAwayPage.class); PaymentPage payment = PageFactory.initElements(driver, PaymentPage.class); AssertFactory Assert = PageFactory.initElements(driver, AssertFactory.class); takeaway.SelectOnlyTimeAndContinue(); takeaway.CompleteCreateYourOrderSection(); takeaway.CompleteReviewOrderSection(); takeaway.CompleteDetailsSection( "Guestfn", "Guestlt", "*****@*****.**", "07511111111", "Address1", "Address2", "Town", "WR9 7PA"); payment.ApprovedPayment(); // Assert.assertTrue(driver.getPageSource().contains("Thank you, ")); takeaway.ConfirmRegistration("password12", "password12", "01/01/1980"); driver.quit(); }
@After public void quitDriver() { if (localDriver != null) { localDriver.quit(); localDriver = null; } }
public static void extractnews(String symbol, String path) throws InterruptedException, IOException { // WebDriver driver = new HtmlUnitDriver(); WebDriver driver = new FirefoxDriver(); driver.get("http://www.otcmarkets.com/stock/" + symbol + "/insider-transactions"); Thread.sleep(3000); boolean next = true; int count = -1; while (next) { next = false; count++; // grab the whole page and the link File file = new File(path + driver.getCurrentUrl().replaceAll("/", "_") + '_' + count); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file); fw.write(driver.getPageSource()); fw.flush(); fw.close(); System.out.println(driver.getCurrentUrl() + '/' + count); // System.out.println(driver.getPageSource()); // check for the next button, if found then click set the flag to true WebElement pagination = null; try { pagination = driver.findElement(By.id("shortInterestTable")).findElement(By.className("pageList")); } catch (Exception e) { continue; } List<WebElement> paginationli = pagination.findElements(By.tagName("li")); for (WebElement ele : paginationli) { try { ele.findElement(By.tagName("a")).getText(); } catch (Exception e) { continue; } if (ele.findElement(By.tagName("a")).getText().equals("next >")) { // System.out.println("HI"); WebElement nextpage = ele.findElement(By.tagName("a")); nextpage.click(); next = true; Thread.sleep(3000); } } } driver.quit(); }
@After public void quitDriver() { if (mAutoQuitDriver && mDriver != null) { mDriver.quit(); mDriver = null; } }
@Test public void canRestartTheIeDriverInATightLoop() { for (int i = 0; i < 5; i++) { WebDriver driver = newIeDriver(); driver.quit(); } }
// @AfterSuite public void resetEnv() { if (driver != null) { driver.quit(); log.info("Browser close successful"); } log.info("Tests Suite execution completed."); }
public static void main(String[] args) throws InterruptedException { WebDriver dr = new ChromeDriver(); File file = new File("src/form.html"); String filePath = "file:///" + file.getAbsolutePath(); System.out.printf("now accesss %s \n", filePath); dr.get(filePath); Thread.sleep(1000); dr.findElement(By.cssSelector("input[type=checkbox]")).click(); Thread.sleep(1000); dr.findElement(By.cssSelector("input[type=radio]")).click(); Thread.sleep(1000); List<WebElement> options = dr.findElement(By.tagName("select")).findElements(By.tagName("option")); options.get(options.size() - 1).click(); Thread.sleep(1000); dr.findElement(By.cssSelector("input[type=submit]")).click(); Alert alert = dr.switchTo().alert(); System.out.println(alert.getText()); alert.accept(); Thread.sleep(1000); System.out.println("browser will be close"); dr.quit(); }
public void destory() { for (WebDriver driver : drivers) { driver.quit(); } delegate = new ThreadLocal<WebDriver>(); drivers = new HashSet<WebDriver>(); }
public void closeBrowser() { driver.manage().deleteAllCookies(); driver.close(); if (driver != null) { driver.quit(); } }
/** 退出Selenium. */ public void quit() { try { driver.quit(); } catch (Exception e) { System.err.println("Error happen while quit selenium :" + e.getMessage()); } }
@AfterTest protected void afterTest(ITestContext context) { WebDriver webdriver = getWebDriverInstance(context); log.info(Utilities.getCurrentThreadId() + "Closing the instance:" + webdriver.toString()); webdriver.quit(); context.removeAttribute(context.getCurrentXmlTest().getName()); }
/** * @param driver the web driver to use for the test * @param wait the implicit wait value for this run * @param waiter handles driver specific waits * @return always returns true */ private boolean childNavigationExercise( final WebDriver driver, final long wait, final PageWaiter waiter) { try { driver.manage().timeouts().implicitlyWait(wait, TimeUnit.SECONDS); // Matthias PersonPage currentPerson = new PersonPage(driver, "I180", null, waiter); currentPerson.open(); assertEquals("Person ID mismatch", "I180", currentPerson.getId()); assertEquals("Person failed check", "", currentPerson.check()); // Johann Martin currentPerson = currentPerson.navigateChild(1, 1); assertEquals("Person ID mismatch", "I3554", currentPerson.getId()); assertEquals("Person failed check", "", currentPerson.check()); // Anna Maria currentPerson = currentPerson.navigateChild(1, 1); assertEquals("Person ID mismatch", "I3881", currentPerson.getId()); assertEquals("Person failed check", "", currentPerson.check()); // Go to Maria Berta Faigle currentPerson = currentPerson.navigateChild(1, 1); assertEquals("Person ID mismatch", "I3891", currentPerson.getId()); assertEquals("Person failed check", "", currentPerson.check()); } finally { // Close the browser driver.quit(); } return true; }
/** * @param driver the web driver to use for the test * @param wait the implicit wait value for this run * @param waiter handles driver specific waits * @return always returns true */ private boolean mothersNavigationExercise( final WebDriver driver, final long wait, final PageWaiter waiter) { try { driver.manage().timeouts().implicitlyWait(wait, TimeUnit.SECONDS); // Melissa PersonPage currentPerson = new PersonPage(driver, "I11", null, waiter); currentPerson.open(); assertEquals("Person ID mismatch", "I11", currentPerson.getId()); assertEquals("Person failed check", "", currentPerson.check()); // Lisa currentPerson = currentPerson.navigateMother(); assertEquals("Person ID mismatch", "I33", currentPerson.getId()); assertEquals("Person failed check", "", currentPerson.check()); // Estelle currentPerson = currentPerson.navigateMother(); assertEquals("Person ID mismatch", "I117", currentPerson.getId()); assertEquals("Person failed check", "", currentPerson.check()); } finally { // Close the browser driver.quit(); } return true; }
@Override public void dismissAll() { for (WebDriver driver : new HashSet<WebDriver>(driverToKeyMap.keySet())) { driver.quit(); driverToKeyMap.remove(driver); } }
private ResultItems myDownload(Request request, Spider spider) throws IOException { ResultItems resultItems = new ResultItems(request, spider); DesiredCapabilities cap = DesiredCapabilities.chrome(); ChromeOptions co = new ChromeOptions(); String userAgent = request.getUserAgent(); co.addArguments("--user-agent=" + userAgent); cap.setCapability(ChromeOptions.CAPABILITY, co); Proxy myProxy = spider.getProxy(request); if (myProxy != null) { String proxy = myProxy.getHost() + ":" + myProxy.getPort(); org.openqa.selenium.Proxy p = new org.openqa.selenium.Proxy(); p.setHttpProxy(proxy).setFtpProxy(proxy).setSslProxy(proxy); cap.setCapability(CapabilityType.PROXY, p); } WebDriver driver = new ChromeDriver(cap); driver.get(request.getUrl()); // Get the html source of the page String pageSource = driver.getPageSource(); // Close the browser driver.quit(); return resultItems.setResource(Jsoup.parse(pageSource, request.getUrl())); }
public static void main(String[] args) { String baseUrl = "http://newtours.demoaut.com/"; WebDriver driver = new FirefoxDriver(); String underConsTitle = "Under Construction: Mercury Tours"; // Creates result string driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get(baseUrl); List<WebElement> linkElements = driver.findElements(By.tagName("a")); // Creates lists from <a> tags String[] linkTexts = new String[linkElements.size()]; int i = 0; // extract the link texts of each link element for (WebElement e : linkElements) { linkTexts[i] = e.getText(); i++; } // test each link by looping over them for (String t : linkTexts) { driver.findElement(By.linkText(t)).click(); if (driver.getTitle().equals(underConsTitle)) { System.out.println("\"" + t + "\"" + " is under construction."); } else { System.out.println( "\"" + t + "\"" // print results to console + " is working."); } driver.navigate().back(); } driver.quit(); }
@Test public void testGoogleTitle() throws Exception { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Cheese!"); element.submit(); System.out.println("Page title is: " + driver.getTitle()); String expected = "Google"; String actual = driver.getTitle(); Assert.assertEquals(actual, expected); (new WebDriverWait(driver, 10)) .until( new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }); System.out.println("Page title is: " + driver.getTitle()); expected = "Cheese! - Google Search"; actual = driver.getTitle(); Assert.assertEquals(actual, expected); driver.quit(); }
@Test public void testLogin() { WebDriver driver = new FirefoxDriver(); LoginPage loginPage = new LoginPage(driver); // UserHomePage userHomePage=new UserHomePage(driver); UserHomePage userHomePage = loginPage.testLogin(); driver.quit(); }
@After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } }
@AfterClass public void afterClass() { System.out.println("After Class method for " + className); Reporter.closeTestSummaryReport(); if (driver != null) { driver.quit(); driver = null; } }
public static void main(String[] args) throws SAXException, IOException, Exception { sendmail mailobj=new sendmail(); mailobj.navigate(); mailobj.logindetails(); mailobj.opencomposeview(); mailobj.emaildetails(); driver.quit(); }
public static void main(String args[]) { // System.setProperty("webdriver.chrome.driver","D:\\Chromedriver\\chromedriver_win32\\chromedriver.exe"); // WebDriver driver= new ChromeDriver(); WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); // Thread.sleep(1000); System.out.println("The title of page is " + driver.getTitle()); driver.quit(); }
public static void closeBrowser(String object, String data) { try { Log.info("Closing the browser"); driver.quit(); } catch (Exception e) { Log.error("Not able to Close the Browser --- " + e.getMessage()); DriverScript.bResult = false; } }
private static void initSeleniumEnd() { LOGGER.info("==============================================================="); LOGGER.info("| Stopping driver (closing browser) |"); LOGGER.info("==============================================================="); driver.quit(); LOGGER.debug("==============================================================="); LOGGER.debug("| Driver stopped (browser closed) |"); LOGGER.debug("===============================================================\n"); }