• 使用Selenium对新浪微博模拟登录


    1. Selenium的配置

    1.1. 在项目中引入Selenium库

    通过Maven加入。

            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.4.0</version>
            </dependency>
    

    1.2. 下载chromedriver.exe

    官方下载连接(需要梯子)

    1.3. 在项目代码中加入chromedriver位置的配置

           System.getProperties().setProperty("webdriver.chrome.driver", "chromedriver.exe");
    

    2. 使用Selenium

    2.1. Selenim语法

    Selenium的语法和一些相关资料可以看这个博客,需要用哪里直接去查阅即可。

    java selenium (一) selenium 介绍

    2.2. 智能等待

    首先阅读这篇java selenium (十三) 智能等待页面加载完成

    由于Java8的lambda表达式可以用于显示等待,以及我个人模拟登陆过程中的一些心得,我这里补充一点内容。sleep()方法和pageLoad方法我就不再介绍,因为不推荐使用。

    2.2.1. 隐式等待

    最常用的等待方式,也比较简单,是全局的。

            webDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    

    在设定的时间内Selenium会尝试获取所要求的元素,很简单,对于简单的查找元素等已经足够。但是如果需要监测一些元素的状态,那还需要调用显示等待的方法。

    2.2.2. 显式等待

    显示等待方法需要构造一个WebDriverWait对象,其接收webDriver和设定时长(单位秒)作为构造器参数构造。

    当需要等待时,则调用它的until方法,until方法接收一个ExceptedCondition<>类对象,这是一个函数对象,因此,我们可以用Java8的lambda表达式去构建。
    这里举例:

    webDriverWait.until((ExpectedCondition<WebElement>) w -> w.findElement(By.xpath("//div[@class='home-sub-nav layout-box']/a[4]"))).click();
    webDriverWait.until((ExpectedCondition<WebElement>) w -> w.findElement(By.xpath("//div[@class='card card2 line-around']/div[1]/a[2]"))).click();
    

    不过,通常情况下,ExceptedConditions类已经定义了我们需要的动作,而不需要自己再去实现这个接口,比如上面例子中的方法就可以用presenceOfElementLocated(By locator)方法去检查,当然,通常依赖隐式等待就可以了。

    这里举一个必须用显示等待的例子,比如微博的登录界面,其输入框在刚开始时就存在,但不可用,我们需要检测到,当它可用时再去运行,这里就需要使用显示等待的方法:

            WebDriverWait webDriverWait=new WebDriverWait(webDriver,10);
            webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginName"))).sendKeys(args[0]);
            webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginPassword"))).sendKeys(args[1]);
    

    此处调用了elementToBeClickable方法,参考文章中有列出ExpectedConditions类中包含的方法,我在这里列出一下:

    等待的条件 ExpectedConditions方法
    页面元素是否在页面上可用和可被单击 elementToBeClickable(By locator)
    页面元素处于被选中状态 elementToBeSelected(WebElement element)
    页面元素在页面中存在 presenceOfElementLocated(By locator)
    在页面元素中是否包含特定的文本 textToBePresentInElement(By locator)
    页面元素值 textToBePresentInElementValue(By locator, java.lang.String text)
    标题 (title) titleContains(java.lang.String title)

    利用这些方法可以很好的监测到我们要的控件是否可用。

    3. 模拟登陆并获取Cookie的代码

    public class WeiboLoginAndGetCookie {
        public static void main(String[] args) throws Exception{
            //配置ChromeDiver
            System.getProperties().setProperty("webdriver.chrome.driver", "chromedriver.exe");
            //开启新WebDriver进程
            WebDriver webDriver = new ChromeDriver();
            //全局隐式等待
            webDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
            //设定网址
            webDriver.get("https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F");
            //显示等待控制对象
            WebDriverWait webDriverWait=new WebDriverWait(webDriver,10);
            //等待输入框可用后输入账号密码
            webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginName"))).sendKeys(args[0]);
            webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("loginPassword"))).sendKeys(args[1]);
            //点击登录
            webDriver.findElement(By.id("loginAction")).click();
            //等待2秒用于页面加载,保证Cookie响应全部获取。
            sleep(2000);
            //获取Cookie并打印
            Set<Cookie> cookies=webDriver.manage().getCookies();
            Iterator iterator=cookies.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next().toString());
            }
            //关闭WebDriver,否则并不自动关闭
            webDriver.close();
        }
    }
    
  • 相关阅读:
    MYSQL利用merge存储引擎来实现分表
    IoC原理及实现
    prometheus + grafana安装部署(centos6.8)
    PHP Laravel定时任务Schedule
    排序算法杂谈(三) —— 归并排序的非递归实现
    排序算法杂谈(二) —— 冒泡排序的递归实现
    排序算法杂谈(一) —— 量化数组的有序程度
    排序算法(七) —— 快速排序
    排序算法(六) —— 归并排序
    排序算法(五) —— 堆排序
  • 原文地址:https://www.cnblogs.com/cielosun/p/7064977.html
Copyright © 2020-2023  润新知