List of usage examples for org.openqa.selenium WebElement getAttribute
String getAttribute(String name);
From source file:ccnu.computer.crawler.WeiboCN.java
/** * ??cookieweibo.cnweibo.com weibo.cn????? * * @param username ???/*from www. ja v a2s. c om*/ * @param password ?? * @return * @throws Exception */ public static String getSinaCookie(String username, String password) throws Exception { StringBuilder sb = new StringBuilder(); HtmlUnitDriver driver = new HtmlUnitDriver(); driver.setJavascriptEnabled(true); driver.get("http://login.weibo.cn/login/"); WebElement ele = driver.findElementByCssSelector("img"); String src = ele.getAttribute("src"); String cookie = concatCookie(driver); HttpRequest request = new HttpRequest(src); request.setCookie(cookie); HttpResponse response = request.getResponse(); ByteArrayInputStream is = new ByteArrayInputStream(response.getContent()); BufferedImage img = ImageIO.read(is); is.close(); ImageIO.write(img, "png", new File("result.png")); String userInput = new CaptchaFrame(img).getUserInput(); WebElement mobile = driver.findElementByCssSelector("input[name=mobile]"); mobile.sendKeys(username); WebElement pass = driver.findElementByCssSelector("input[name^=password]"); pass.sendKeys(password); WebElement code = driver.findElementByCssSelector("input[name=code]"); code.sendKeys(userInput); WebElement rem = driver.findElementByCssSelector("input[name=remember]"); rem.click(); WebElement submit = driver.findElementByCssSelector("input[name=submit]"); submit.click(); String result = concatCookie(driver); driver.close(); if (result.contains("gsid_CTandWM")) { return result; } else { throw new Exception("weibo login failed"); } }
From source file:ch.admin.isb.hermes5.common.AbstractPageDriver.java
License:Apache License
public WebElement selectOptionWithValue(WebElement selectOneMenu, String value) { for (WebElement option : allOptions(selectOneMenu)) { if (option.getAttribute("value").equals(value)) { return option; }/* ww w.j a va2 s. c o m*/ } throw new NoSuchElementException(value + " in " + selectOneMenu + "\n" + driver.getPageSource()); }
From source file:ch.vorburger.mifos.wiki.ZWikiScraper.java
License:Apache License
private List<WikiNode> ulToNodes(WebElement subElements, WikiNode parent) { List<WikiNode> nodes = new LinkedList<ZWikiScraper.WikiNode>(); List<WebElement> lis = subElements.findElements(By.xpath("li")); for (WebElement li : lis) { WebElement ahref = li.findElement(By.tagName("span")).findElement(By.tagName("a")); String href = ahref.getAttribute("href"); String pageID = wikiURL2PageId(href); WikiNode n = new WikiNode(); n.pageID = pageID;// w w w . java 2s. com n.parentNode = parent; try { WebElement ul = li.findElement(By.tagName("ul")); n.childNodes.addAll(ulToNodes(ul, n)); } catch (NoSuchElementException e) { // No children then, OK. } nodes.add(n); } return nodes; }
From source file:ch.vorburger.webdriver.reporting.LoggingWebDriverEventListener.java
License:Apache License
/** * Log, with a screenshot.//from w w w. ja v a2 s . co m * * @param element the WebElement, to give context, can be null if the previous message already gave it * @param message the message to log, never null * @param screenshot the Screenshot File, can be null if no screenshot is to be logged. The File is copied. */ private void log(WebElement element, String message, File screenshot) { StringBuilder sb = new StringBuilder(); sb.append(message); if (element != null) { sb.append(" "); String userVisibleLabel = getUserVisibleText(element); if (userVisibleLabel != null) { sb.append("'" + userVisibleLabel + "', "); } sb.append("element"); if (getAttributeSafely(element, "id") != null) { String id = getAttributeSafely(element, "id"); sb.append(" with ID " + id); } // Show the name only if there is no ID (id is stronger) else if (getAttributeSafely(element, "name") != null) { sb.append(" with name " + element.getAttribute("name")); } } clsObj.infoWithFlagAndScreenshot(sb.toString(), screenshot); }
From source file:ch.vorburger.webdriver.reporting.LoggingWebDriverEventListener.java
License:Apache License
private String getAttributeSafely(WebElement element, String attributeName) { try {//from w w w . ja v a 2 s . c om return element.getAttribute(attributeName); } catch (WebDriverException e) { // If we couldn't get the attribute, something is wrong, it probably // doesn't have one, so let's just return null: return null; } }
From source file:ch.vorburger.webdriver.reporting.LoggingWebDriverEventListener.java
License:Apache License
public void addStyleBeforeSnapShot(WebElement element, WebDriver driver) { String webElementId;/*from w w w . ja v a 2s. c om*/ if (element != null) { try { webElementId = element.getAttribute("id"); } catch (StaleElementReferenceException e) { webElementId = null; } if (webElementId != null && !webElementId.isEmpty() && driver instanceof JavascriptExecutor) { try { ((JavascriptExecutor) driver).executeScript( "document.getElementById('" + webElementId + "').setAttribute('style','border:solid 2px #73A6FF;background:#EFF5FF;')", ""); } catch (Throwable e) { // Highlight ON didn't work, tant pis. } } } }
From source file:cls.ui.model.selenium.secondhandcarmanagement.StartAssessmentPage.java
public void setAssessmentCode() { WebElement codeInput = driver.findElement(By.name("estimateNum")); assessmentCode = codeInput.getAttribute("value"); }
From source file:cn.edu.bistu.spider.sina.WeiboCN.java
/** * ??cookieweibo.cnweibo.com weibo.cn???? * @param username ???/*from ww w .ja va2 s . co m*/ * @param password ?? * @return * @throws Exception */ @SuppressWarnings("deprecation") public static String getSinaCookie(String username, String password) throws Exception { String result = ""; HtmlUnitDriver driver = new HtmlUnitDriver(); while (!result.contains("gsid_CTandWM")) { driver.setJavascriptEnabled(true); driver.get("http://login.weibo.cn/login/"); WebElement ele = driver.findElementByCssSelector("img"); String src = ele.getAttribute("src"); String cookie = concatCookie(driver); HttpRequest request = new HttpRequest(src); request.setCookie(cookie); HttpResponse response = request.getResponse(); ByteArrayInputStream is = new ByteArrayInputStream(response.getContent()); BufferedImage img = ImageIO.read(is); is.close(); ImageIO.write(img, "png", new File("result.png")); String userInput = new CaptchaFrame(img).getUserInput(); WebElement mobile = driver.findElementByCssSelector("input[name=mobile]"); mobile.sendKeys(username); WebElement pass = driver.findElementByCssSelector("input[name^=password]"); pass.sendKeys(password); WebElement code = driver.findElementByCssSelector("input[name=code]"); code.sendKeys(userInput); WebElement rem = driver.findElementByCssSelector("input[name=remember]"); rem.click(); WebElement submit = driver.findElementByCssSelector("input[name=submit]"); submit.click(); result = concatCookie(driver); } driver.close(); return result; }
From source file:cn.edu.hfut.dmic.webcollector.example.QianQuCrawler.java
License:Open Source License
public void startS() { String url = null;/*from www.j a v a 2 s. c o m*/ try { url = createBingUrl("?", 1); } catch (Exception e) { e.printStackTrace(); } Executor executor = new Executor() { @Override public void execute(CrawlDatum page, CrawlDatums next) throws Exception { String pageType = page.meta("pageType"); HtmlUnitDriver driver = new HtmlUnitDriver(); driver.setJavascriptEnabled(true); driver.get(page.getUrl()); WebElement element = driver.findElementByCssSelector("div#mainContent"); List<WebElement> elementss = element.findElements(By.cssSelector("div.article")); for (WebElement element0 : elementss) { WebElement webElementa = element0.findElement(By.cssSelector("img")); WebElement webElementb = element0.findElement(By.cssSelector("h3 a")); WebElement webElementc = element0.findElement(By.cssSelector("p")); WebElement webElementd = element0.findElement(By.cssSelector("a")); String href = webElementa.getAttribute("src"); String link = webElementd.getAttribute("href"); CrawlDatum crawlDatum = new CrawlDatum(link).meta("Title", webElementb.getText()) .meta("Image", href).meta("Description", webElementc.getText()) .meta("pageType", "outlink").meta("link", link); QianQuCrawler crawler = new QianQuCrawler("bcrawlerd", true, link, request); crawler.addSeed(crawlDatum); crawler.setThreads(30); crawler.start(1); } for (int i = 2; i < 3; i++) { // driver.findElement(By.cssSelector("a.page-link.next")).click(); //?? Thread.sleep(waitLoadBaseTime + random.nextInt(waitLoadRandomTime)); WebElement element2 = driver.findElementByCssSelector("div#mainContent"); List<WebElement> elementss2 = element2.findElements(By.cssSelector("div.article")); for (WebElement element0 : elementss2) { WebElement webElementa = element0.findElement(By.cssSelector("img")); WebElement webElementb = element0.findElement(By.cssSelector("h3 a")); WebElement webElementc = element0.findElement(By.cssSelector("p")); WebElement webElementd = element0.findElement(By.cssSelector("a")); String href = webElementa.getAttribute("src"); String link = webElementd.getAttribute("href"); CrawlDatum crawlDatum = new CrawlDatum(link).meta("Title", webElementb.getText()) .meta("Image", href).meta("Description", webElementc.getText()) .meta("pageType", "outlink").meta("link", link); QianQuCrawler crawler = new QianQuCrawler("bcrawlerd", true, link, request); crawler.addSeed(crawlDatum); crawler.setThreads(30); crawler.start(5); } Thread.sleep(waitLoadBaseTime + random.nextInt(waitLoadRandomTime)); } } }; //DBDBManager DBManager manager = new BerkeleyDBManager("acrawlerd"); //Crawler?DBManagerExecutor Crawler crawler = new Crawler(manager, executor); crawler.setThreads(30); crawler.addSeed(url); try { crawler.start(1); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.amolik.scrapers.OdishaRationCardScraper.java
public static void processDistrict(WebDriver driver, int districtIndex) { Select districtSelect = new Select(driver.findElement(By.name(Constants.DDL_DISTRICT))); districtSelect.selectByIndex(districtIndex); try {/*from w w w . j av a 2 s .c o m*/ TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int blockSize = getRefreshedBlockSelect(driver, districtIndex).getOptions().size(); if (logger.isDebugEnabled()) { logger.debug("processDistrict blockSize=" + blockSize); //$NON-NLS-1$ } // Remove during production //blockSize =2; int startBlockIndex = new Integer(AmolikProperties.getProperty("odisha_ration.startBlockIndex")).intValue(); ArrayList<OdishaRationCardBean> beanList = new ArrayList(); for (int blockIndex = startBlockIndex; blockIndex < blockSize; blockIndex++) { Select blockSelect = getRefreshedBlockSelect(driver, districtIndex); List<WebElement> blocksList = blockSelect.getOptions(); WebElement block = blocksList.get(blockIndex); if (logger.isDebugEnabled()) { logger.debug("processDistrict(WebDriver, int) - block=" + block.getText()); //$NON-NLS-1$ } String blockValue = block.getAttribute(Constants.LOWERCASE_VALUE); if (logger.isDebugEnabled()) { logger.debug("processDistrict(WebDriver, int) - " + districtIndex + "|" + block.getAttribute(Constants.LOWERCASE_VALUE) + "|" + block.getText()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } processBlock(driver, districtIndex, blockIndex, blockValue, block.getText(), beanList); } // Create new Directory if not exists String districtName = districtsNameList.get(districtIndex); String excelDestDirName = AmolikProperties.getProperty("odisha_ration.excelOutputDir"); new File(excelDestDirName).mkdirs(); String excelDestFileName = excelDestDirName + System.getProperty("file.separator") + districtName + excelFileExtension; if (logger.isInfoEnabled()) { logger.info("writing to excel " + districtIndex + "|" + districtName + "| toal recordCount=" + (beanList.size())); } ExcelUtil.generateExcelFromBean(beanList, excelTemplateFileName, excelTemplateFileName, excelDestFileName, "rationCard"); }