Selenium用法详解【窗口表单切换】【JAVA爬虫】

简介

本文主要讲解java 代码利用Selenium如何实现控制浏览器进行窗口切换和页面内的不同表单之间的切换操作。

切换操作

窗口切换

selenium 操作页面的时候,可能会因为点击某个链接而跳转到一个新的页面(打开了一个新标签页),这时候 selenium 实际还是处于上一个页面的,需要我们进行切换才能够定位最新页面上的元素。

窗口切换需要使用 switch_to.windows() 方法。

首先我们先看看下面的代码。

代码流程:先进入 【CSDN首页】,保存当前页面的句柄,然后再点击头条的首篇文章,跳转进入新的标签页,再次保存页面的句柄,我们验证一下 selenium 会不会自动定位到新打开的窗口。


import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.IOException;

public class SeleniumDemo {
    private final static String webDriver = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";


    public static void main(String[] args) throws InterruptedException, IOException {
        System.setProperty(webDriver, webDriverPath);
        WebDriver driver= new ChromeDriver();
        //进入csdn首页
        driver.get("https://www.csdn.net/");
        Thread.sleep(1000);
        //定位头条元素
        WebElement element=driver.findElement(By.xpath("//div[@class='headlines-left']/dl/dt/a"));
        //点击进入头条
        element.click();
        //获取当前窗口句柄
        System.out.println(driver.getWindowHandle());
        //获取所有窗口句柄
        System.out.println(driver.getWindowHandles());
    }
}

HANDLE:句柄,是Windows用来表示对象的

可以看到第一个列表 WindowHandle 是相同的,说明 selenium 实际操作的还是 CSDN首页 ,并未切换到新页面。

下面使用 switchTo().window() 进行切换。

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SeleniumDemo {
    private final static String webDriver = "webdriver.chrome.driver";
    private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";


    public static void main(String[] args) throws InterruptedException, IOException {
        System.setProperty(webDriver, webDriverPath);
        WebDriver driver= new ChromeDriver();
        //进入csdn首页
        driver.get("https://www.csdn.net/");
        Thread.sleep(1000);
        //定位头条元素
        WebElement element=driver.findElement(By.xpath("//div[@class='headlines-left']/dl/dt/a"));
        //点击进入头条
        element.click();
        //获取当前窗口
        System.out.println(driver.getWindowHandle());
        //获取所有窗口
        System.out.println(driver.getWindowHandles());
        List<String> windows= new ArrayList<>(driver.getWindowHandles());
         driver.switchTo().window(windows.get(windows.size()-1));
        //获取当前窗口
        System.out.println(driver.getWindowHandle());
    }
}

上面代码在点击跳转后,使用 switchTo 切换窗口,getWindowHandles 返回的 handle 列表是按照页面出现时间进行排序的,最新打开的页面肯定是最后一个,这样用 windows.get(windows.size()-1)即可跳转到最新打开的页面了。

那如果打开的窗口有多个,如何跳转到之前打开的窗口,如果确实有这个需求,那么打开窗口是就需要记录每一个窗口的 key(别名)value(handle),保存到字典中,后续根据 key 来取 handle

表单切换

很多页面也会用带 frame/iframe 表单嵌套,对于这种内嵌的页面 selenium 是无法直接定位的,需要使用 switchTo().frame() 方法将当前操作的对象切换成 frame/iframe 内嵌的页面。

switchTo().frame() 默认可以用的 idname 属性直接定位,但如果 iframe 没有 idname ,这时就需要使用 xpath 进行定位。下面先写一个包含 iframe 的页面做测试用。

A:<iframe id="index" name="home" src="/main.html" frameborder="0" scrolling="auto" ></iframe>
B:<iframe id="baidu" name="baidu" src="http://www.baidu.com" frameborder="0" scrolling="auto" ></iframe>
  //根据排序定位frame切换
  driver.switchTo().frame(1);
 //根据frame的name或者id定位切换
  driver.switchTo().frame("baidu");

frame表单切换,在以前前端没有分离前还是经常见到的,自从前后端分离后,前端技术发生大变化,如今用frame的页面已经很少见,出来用switchTo().frame() 切换换外,我们也可以通过元素定位点击时间进行切换,到达了灵活使用的目的。

猜你喜欢

转载自blog.csdn.net/weixin_40986713/article/details/128590061