0
点赞
收藏
分享

微信扫一扫

3.2 动态规划算法的基本要素

40dba2f2a596 2023-06-04 阅读 52

一.Selenium含义
Selenium是用来做web自动化的测试框架,它的特点是支持各种浏览器,支持各种平台和语言

二.Selenium项目的创建

1.创建maven项目

2.添加Selenium相关依赖

  <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

三.Selenium元素的定位主要有两种方式:

1.Css选择器

css 选择语法

id选择器:#id

类选择:.class

标签选择:标签名

后代选择器:父级选择器、子级选择器

具体操作实现:

以定位百度首页输入框为例

(1)点击f12打开开发者工具

(2)

2.

2.xpath

分为绝对路径和相对路径

绝对路径:/html/head/title  定位元素效率较低,一般不用

相对路径:相对路径+索引:  //form/span[1]/input

                 相对路径+属性值://input[@class="s_ipt"]

                  相对路径+通配符://*[@*="su"]

                    相对路径+文本匹配;//a[text()="视频"]

css选择器和xpath选择器那个更好?

css选择器定位元素效率更高

四.Selenium相关操作

1.click点击对象

2.send_keys在对象上模拟按键输入

3.clear清除对象输入的文本内容

4.submit提交

5.gettext()用于获取文本信息

click和submit的区别:如果点击的元素放在form标签中,此时两者效果一致,如果点击的元素放在非form标签中,此时用submit会报错

 public static void test1() throws InterruptedException {

        WebDriver webDriver = new ChromeDriver();
        //得到百度页面
        webDriver.get("https://www.baidu.com");
        WebElement element = webDriver.findElement(By.cssSelector("#kw"));
        //在输入框输入软件测试
        element.sendKeys("软件测试");
        //点击百度一下按钮
        webDriver.findElement(By.cssSelector("#su")).click();
        //获取当前页面的url
        String url = webDriver.getCurrentUrl();
        //获取当前页面的标题
        String title = webDriver.getTitle();
        sleep(3000);
        //清空输入框内容
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

浏览器的操作:

 private static void test7() throws InterruptedException {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //浏览器后退
        webDriver.navigate().back();
        sleep(3000);
        //刷新当前页面
        webDriver.navigate().refresh();
        sleep(3000);
        //浏览器前进
        webDriver.navigate().forward();
        sleep(3000);
        //浏览器最大化显示
        webDriver.manage().window().maximize();
        sleep(6000);
        //将浏览器的滚动条滑到最下端
        ((JavascriptExecutor) webDriver).executeScript("document.documentElement.scrollTop=10000");
        sleep(3000);
        //浏览器全屏
        webDriver.manage().window().fullscreen();
        sleep(3000);
        //设置浏览器窗口的大小
        webDriver.manage().window().setSize(new Dimension(60, 100));

    }

close和quit的区别:

 webDriver.quit();
 webDriver.close();

 sleep和智能等待

sleep是死等

智能等待分为:隐式等待、显示等待

隐式等待:

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

等待所有页面元素都出来,才会执行下面代码,可设定最大等待时间,如果超出了最大等待间页面的全部元素还没有加载出来,也会执行下面代码

private static void test06() {
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com/");   
        WebDriverWait wait = new WebDriverWait(webDriver, 1);
        //等待百度一下,你就知道这个页面title加载出来
        wait.until(ExpectedConditions.titleIs("百度一下,你就知道"));

    }

显示等待:可以指定具体的页面元素,等这个具体的页面元素加载出来,执行下面代码,也有最大等待时间,如果超出了最大等待时间,指定的具体的页面元素还没加载出来,也会执行下面代码

close:只是关闭当前页面,不会清空缓存

quit:关闭所有页面,清空缓存

 键盘事件:

send_keys(Keys.CONTROL,'a') # 全选( Ctrl+A

send_keys(Keys.CONTROL,'c') # 复制( Ctrl+C

send_keys(Keys.CONTROL,'x') # 剪贴( Ctrl+X

send_keys(Keys.CONTROL,'v') # 粘贴( Ctrl+V

  private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://wwww.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("测试");
        sleep(3000);
        //表示全选当前输入框内容
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        sleep(3000);
        //剪切当前输入框内容
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        sleep(3000);
        //赋值当前输入框内容
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");


    }

鼠标事件:

context_click() 右击

double_click() 双击

drag_and_drop() 拖动

move_to_element() 移动

private static void test9() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://wwww.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //找到图片按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej"));
        Actions actions = new Actions(webDriver);
        //将鼠标定位到图片上,并右击
        actions.moveToElement(webElement).contextClick().perform();


    }

页面的切换:

默认的定位元素的方式是在get页面(也就是我们一开始打开的页面)获取的,如果想在其他页面定位元素,此时需要切换页面,拿到目标

  private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        // 通过getWindowHandles获取所有的窗口句柄
        // 通过getWindowHandle获取的get打开的页面窗口句柄
        System.out.println(webDriver.getWindowHandle());
        Set<String> handles = webDriver.getWindowHandles();
        //获取到目标页面的窗口句柄
        String target_handle = "";
        for(String handle:handles) {
            target_handle = handle;
        }
        //切换到目标页面
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("测试");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();

    }

页面的窗口句柄,然后切换到目标页面。窗口句柄是一个唯一标识一个窗口的整数值,它可以用来在程序中操作窗口。每个窗口都有一个唯一的窗口句柄,可以通过调用Windows API函数获取。窗口句柄可以用来发送消息到窗口、获取和设置窗口的属性、以及控制窗口的行为。在Windows操作系统中,窗口句柄是一个非常重要的概念,它是实现GUI程序的关键之一。

比如我们想在百度新闻的搜索框里输入“测试”两个字样:

 

选复选框

private static void test8() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\MyDrivers\\backup\\untitled12\\src\\main\\Page\\test01.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            // 如果元素type值等于checkbox进行点击
            // getAttribute获取页面上的元素属性值,里面的type是当前元素属性
            if (webElements.get(i).getAttribute("type").equals("checkbox")) {
                webElements.get(i).click();
            } else {
                // 否则什么也不操作
                ;
            }
        }
}

多层框架的定位:

此时如果我们通过直接通过选择器定位click的方式是获取不到的

 private static void page02() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test02.html?_ijt=arpr09o5r3gegeidj4o2r6hc9b&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

这里就涉及到多层次框架定位的问题了

对于一个 web 应用,经常会出现框架( frame ) 或窗口( window )的应用,这也就给我们的定位带来

了一定的困难。

定位一个 frame switch_to.frame(name_or_id_or_frame_element)

定位一个窗口 window switch_to.window(name_or_id_or_frame_element)

多层次框架的定位:

switch_to.frame(name_or_id_or_frame_element) :通过 frame id 或者 name 或者 frame 自带的其它

属性来定位框架,这里 switch_to.frame() 把当前定位的主体切换了 frame 里。

switch_to.default_content :从 frame 中嵌入的页面里跳出,跳回到最外面的默认页面中。

 private static void page02() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test02.html?_ijt=arpr09o5r3gegeidj4o2r6hc9b&_ij_reload=RELOAD_ON_SAVE");
         //先定位到frame框架
        webDriver.switchTo().frame("f1");
        //然后再定位click元素
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

下拉框选择: 


    private static void test10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("C:\\MyDrivers\\backup\\untitled12\\src\\main\\Page\\test03.html");
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        sleep(3000);
        Select select = new Select(webElement);
        //选中11.61这个下拉框一共有两种选择,一个是下标
        select.selectByIndex(1);
        //另一个是值
        select.selectByValue("11.61");
    }

 弹窗

 private static void test13() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("file:///C:/MyDrivers/backup/untitled12/src/main/Page/test04.html");
        //sleep(3000);

        sleep(3000);
        webDriver.findElement(By.cssSelector("button")).click();
        //alert取消弹窗
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        webDriver.findElement(By.cssSelector("button")).click();
        sleep(3000);
        //在弹窗里输入信息
        webDriver.switchTo().alert().sendKeys("小凯");
        sleep(6000);
        //alert弹窗确认
        webDriver.switchTo().alert().accept();
    }

上传文件 

上传文件

 截图:

private static void test16() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("截图");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(file, new File("D:\\123\\C\\jietu.png"));

    }

举报

相关推荐

0 条评论