看到这里,我就不介绍太多的selenium是什么了
需要的jar包和常见问题在个人分类的其他文章中
demo中有phantomjs,无浏览器访问,相关文章请看个人博客爬虫分类
另外(访问像微博https://weibo.com/这样的网站,用到...driver.findElement(By.className("list_title_s"));//查找这个class,让程序等待需要的内容加载完成...)(第二篇代码)
package net.domain;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumDynamicWeb {
  public static void main(String[] args) {
    Search();
    //System.setProperty("phantomjs.binary.path", "tools\\phantomjs.exe");//无浏览器访问
    System.setProperty("webdriver.chrome.driver", "tools\\chromedriver.exe");// chromedriver服务地址
    WebDriver driver = new ChromeDriver(); // 新建一个WebDriver 的对象,但是new
                        // 的是FirefoxDriver的驱动
    driver.get("http://www.baidu.com");// 打开指定的网站
    driver.findElement(By.id("kw")).sendKeys(
        new String[] { "rodert_wang csdn" });// 找到kw元素的id,然后输入hello
    driver.findElement(By.id("su")).click(); // 点击按扭
    try {
      /**
       * WebDriver自带了一个智能等待的方法。
       * dr.manage().timeouts().implicitlyWait(arg0, arg1);
       * Arg0:等待的时间长度,int 类型 ; Arg1:等待时间的单位 TimeUnit.SECONDS 一般用秒作为单位。
       */
      driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    } catch (Exception e) {
      e.printStackTrace();
    }
    /**
     * dr.quit()和dr.close()都可以退出浏览器,简单的说一下两者的区别:第一个close,
     * 如果打开了多个页面是关不干净的,它只关闭当前的一个页面。第二个quit,
     * 是退出了所有Webdriver所有的窗口,退的非常干净,所以推荐使用quit最为一个case退出的方法。
     */
    driver.quit();// 退出浏览器
    liulanqi();
  }
  private static void liulanqi() {
    /**
     * 操作浏览器 1、浏览器窗口最大化 2、浏览器前进 3、浏览器后退 4、浏览器刷新 5、浏览器切换窗口
     * 
     * @author https://me.csdn.net/qq_40374604
     *
     */
    String url = "https://blog.csdn.net/qq_40374604/article/details/83996505";
    System.setProperty("webdriver.chrome.driver", "tools\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get(url);
    // 1
    driver.manage().window().maximize();
    driver.findElement(By.linkText("rodert_wang")).click();
    driver.navigate().refresh();// 刷新页面
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    driver.navigate().back();// 回退
    driver.navigate().forward();// 前进
    driver.quit();
  }
  private static void Search() {
    /**
     * 获取http://www.com页面中,所有</a>标签"href"属性值
     * 包含英文单词“place”的URL,并将结果保存到“d://logSelenim.log”文件中。
     * 
     * @author https://me.csdn.net/qq_40374604
     *
     */
    String url = "https://me.csdn.net/qq_40374604";
    System.setProperty("webdriver.chrome.driver", "tools\\chromedriver.exe");
    File file = new File("d://logSelenim.log");
    if (!(file.exists())) {
      try {
        file.createNewFile();
      } catch (IOException e) {
        System.out.println("创建文件失败");
        e.printStackTrace();
      }
    }
    WebDriver driver = new ChromeDriver();
    driver.get(url);
    List<WebElement> aList = driver.findElements(By.tagName("a"));
    FileOutputStream fs;
    try {
      fs = new FileOutputStream(file);
      for (WebElement a : aList) {
        String urlStr = a.getAttribute("href");
        System.out.println(urlStr);
        if (urlStr.contains("qq_40374604/article")) {
          urlStr += "\r\n";
          fs.write(urlStr.getBytes());
        }
      }
    } catch (Exception e) {
      System.out.println("文件写入失败");
      e.printStackTrace();
    }finally{
      driver.quit();
    }
  }
}package cn.server;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.google.gson.JsonArray;
public class ModelSelenium {
  public static String Search(String url) throws InterruptedException {
    /**
     * 基于selenium
     * 
     * @author wangshiyu
     */
    System.setProperty("webdriver.chrome.driver", "tools\\chromedriver.exe");// chromedriver服务地址
    WebDriver driver = new ChromeDriver();// 新建一个WebDriver 的对象,但是new
    // System.setProperty("phantomjs.binary.path", "tools\\phantomjs.exe");
    // WebDriver driver = new PhantomJSDriver();
    driver.get(url);
    driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
    driver.findElement(By.className("list_title_s"));//查找这个class,让程序等待内容加载
    String pageSource = driver.getPageSource();
    driver.manage().window().maximize();
    Thread.sleep(1000);
    getLink(pageSource, url);
    System.out.println(pageSource);
    // driver.quit();
    return pageSource;
  }
  public static JsonArray getLink(String html, String baseUri) {
    JsonArray seedsJsonArray = new JsonArray();
    Document doc = Jsoup.parse(html, baseUri);
    Elements links = doc.select("a[href]");
    Elements media = doc.select("[src]");
    for (Element element : links) {
      String hrefString = element.attr("abs:href");
      if (hrefString.length() > 5) {
        seedsJsonArray.add(hrefString.toString());
      }
    }
    for (Element element : media) {
      String srcString = element.attr("abs:src");
      if (srcString.length() > 5) {
        seedsJsonArray.add(element.attr("abs:src").toString());
      }
    }
    System.out.println("#######" + seedsJsonArray);
    return seedsJsonArray;
  }
  ("finally")
  public static String getAbsoluteURL(String baseURI, String relativePath) {
    String abURL = null;
    try {
      URI base = new URI(baseURI);// 基本网页URI
      URI abs = base.resolve(relativePath);// 解析于上述网页的相对URL,得到绝对URI
      URL absURL = abs.toURL();// 转成URL
      abURL = absURL.toString();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (URISyntaxException e) {
      e.printStackTrace();
    } finally {
      return abURL;
    }
  }
  public static void main(String[] args) {
    try {
      String url = "https://weibo.com/";
      Search(url);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}









