selenium+java-find URLs that contain keywords in a page

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.