Package Seleniumlearn1;import Java.io.file;import Java.io.fileoutputstream;import java.io.ioexception;import Java.util.list;import Org.openqa.selenium.by;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.chrome.chromedriver;import com.gargoylesoftware.htmlunit.javascript.host.element;/** * Get http://www.qyer.com page, all </a> label "href" attribute values * Contains the URL for the English word "place" and saves the result to the "/home/result.log" file. * @author 0 * */public class Search {static String baseurl= "http://www.qyer.com";p ublic static void Main (string[] args) {S Ystem.setproperty ("Webdriver.chrome.driver", "E:\\\\webdriver\\\\chromedriverv2.28.exe"); File LogFile = new file ("D://logfile.txt"); Logfile.exists ())) {try {logfile.createnewfile ()} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Webdriver Driver = new Chromedriver ();d river.get (BASEURL);//Get all a tags list<webelement> alist = driver.findelements (By.tagname ("a")); try {thread.sleep (10000);} catch (Exception e) {e.printstacktrace ();} Convenient all tags FileOutputStream fs = null;try {fs = new FileOutputStream (logFile); for (Webelement a:alist) {System.out.println ( A.getattribute ("href"));//Get url//in a tag get a tag href attribute value string urlstr = A.getattribute ("href"); if (Urlstr.contains (" Place ") {urlstr =" \ r \ n ";//Writes the URL to the file Fs.write (Urlstr.getbytes ());}}} catch (Exception e) {e.printstacktrace ();}}}
selenium+java-find URLs that contain keywords in a page