zoukankan      html  css  js  c++  java
  • selenium+java-查找页面中包含关键字的URL

    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;
    
    /**
     * 获取http://www.qyer.com页面中,所有</a>标签"href"属性值
     * 包含英文单词“place”的URL,并将结果保存到“/home/result.log”文件中。
     * @author 0
     *
     */
    public class Search {
    	
    	static String baseUrl="http://www.qyer.com";
    	public static void main(String[] args) {
    		System.setProperty("webdriver.chrome.driver", "E:\\webDriver\\chromedriverV2.28.exe");
    		File logFile = new File("d://logFile.txt");
    		if(!(logFile.exists())) {
    			try {
    				logFile.createNewFile();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		
    		WebDriver driver = new ChromeDriver();
    		driver.get(baseUrl);
    		
    		//获取所有a标签
    		List<WebElement> aList = driver.findElements(By.tagName("a"));
    		try {
    			Thread.sleep(10000);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		//便利所有标签
    		FileOutputStream fs = null;
    		try {
    			fs = new FileOutputStream(logFile);
    			for (WebElement a : aList) {
    				System.out.println(a.getAttribute("href"));//获取a标签中的URL
    				
    				//获取a标签href属性值
    				String urlStr = a.getAttribute("href");
    				if(urlStr.contains("place")) {
    					urlStr +="
    ";
    					//将URL写入文件中
    					fs.write(urlStr.getBytes());
    				}
    				
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

      

  • 相关阅读:
    【计算机世界】467- XOR — 神奇的按位运算符
    记 · 复习知识 · 偶遇好玩的知识点
    【CSS】466- 一行 CSS 代码搞定响应式布局
    【Web技术】465- 关于前端埋点统计方案思考
    【CSS】464- 5种 CSS 浮动和清除浮动的方法
    简单易懂的 React useState() Hook 指南(长文建议收藏)
    java中的四类八种
    线程
    异常
    Aspx Ajax 调用 C#函数处理数据
  • 原文地址:https://www.cnblogs.com/sincoolvip/p/7491043.html
Copyright © 2011-2022 走看看