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

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

  • 相关阅读:
    企业信息开发平台(5)流程设计(一)
    企业信息开发平台(6)Web表单设计器开源
    Guava的常用方法示例
    apk反编译
    公司注册登记流程
    Git 使用流程
    ZIP压缩和解压字符串
    vue+elementui实现无限级动态菜单树
    vue 开发笔记
    从零到一开发博客后台管理系统(一)
  • 原文地址:https://www.cnblogs.com/dreamyu/p/6408017.html
Copyright © 2011-2022 走看看