zoukankan      html  css  js  c++  java
  • 一个selenium笔试题——去哪网首页获取符合要求的url并保存

         今天在群里看到这样一个笔试题:请使用任何熟悉的面向对象编程语言,编写代码,获取http://www.qyer.com页面中,所有</a>标签"href"属性值包含英文单词“place”的URL,并将结果保存到“/home/result.log”文件中。

         思路:用selenium定位获得所有</a>标签包含“href”属性的元素,然后遍历,符合要求的就写入到/home/result.log文件中。

        

    package com.testngDemo;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.OutputStreamWriter;
    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class SeleniumTest {
    
        static String baseUrl = "http://www.qyer.com";
        public static void main(String args[])
        {
            //创建个log文件
            File logFile = new File("d://logFile.txt");
            if(!logFile.exists())
            {
                try {
                    logFile.createNewFile();
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
            //创建一个webdriver
            WebDriver driver = new FirefoxDriver();
            driver.get(baseUrl);
            
            //设置等待
            try {
                Thread.sleep(10000);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            //获得所有a标签
            List<WebElement> aList = driver.findElements(By.tagName("a"));
            try {
                Thread.sleep(10000);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            //遍历所有标签 
            FileOutputStream fs = null;
            try {
                fs = new FileOutputStream(logFile);
                for(WebElement a:aList){
                    System.out.println(a.getAttribute("href"));
                    
                    //获得a标签href属性
                    String urlStr = a.getAttribute("href");
                    if(urlStr.contains("place"))
                    {
                        urlStr+="
    ";
                        //将url写入文件中
                        fs.write(urlStr.getBytes());
                    }
                }
                
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }finally{
                try {
                    fs.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            }    
        }
    }

    查看下log文件:

    ------------------------------------------------------时间匆忙,随便写了下,有时间再优化,重要的是思路吧----------------------------------------

  • 相关阅读:
    oracle 排序字段自增长
    ORACLE REPLACE函数
    oracle非空不做更新
    Elasticsearch 5.4.3 聚合分组
    Elasticsearch 版本控制
    Elasticsearch 配置同义词
    Elasticseach的评分机制
    实现Ecshop商品跳到淘宝、京东等的购买链接
    修改ECSHOP系统红包序列号规律
    Ecshop后台订单列表增加”商品名”检索字段
  • 原文地址:https://www.cnblogs.com/dreamyu/p/6408017.html
Copyright © 2011-2022 走看看