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

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

  • 相关阅读:
    微信支付-微信公众号支付,微信H5支付,微信APP支付,微信扫码支付
    微信公众号支付开发当前URL未注册解决办法
    程序员头像
    jQuery简单实现iframe的高度根据页面内容自适应的方法(转)
    MySQL大表优化方案 Mysql的row_format(fixed与dynamic)
    PHP 测试杂项
    【转】MySQL的学习--触发器
    MYSQL PROCEDURE 测试用例
    CSS好看的按钮
    表格可在线编辑效果
  • 原文地址:https://www.cnblogs.com/dreamyu/p/6408017.html
Copyright © 2011-2022 走看看