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文件:

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

  • 相关阅读:
    SpringBoot集成Swagger2并配置多个包路径扫描
    SpringBoot基于EasyExcel解析Excel实现文件导出导入、读取写入
    SpringBoot + SpringSecurity + Quartz + Layui实现系统权限控制和定时任务
    Redis数据持久化(RDB、AOF)
    so安全编译选项(栈溢出保护)
    Python调用Chrome下载文件
    AtomicInteger如何保证线程安全以及乐观锁/悲观锁的概念
    SpringCloud:Eureka的健康检测机制
    共享锁(S锁)和排它锁(X锁)
    Collection接口及其常用子类(java.util包)
  • 原文地址:https://www.cnblogs.com/dreamyu/p/6408017.html
Copyright © 2011-2022 走看看