java-selenium学习(基础之基础,笔者也是最近刚刚涉猎正在深入学习)

Selenium-Java_day_01—①测试打开百度

引入edge的驱动

 [msedgedriver.exe](E:\新疆师范大学研究生学习\selenium\msedgedriver.exe) 
 1.创建src下面的drivers文件,将驱动文件放进去

利用@BeforeClass和@AfterClass实现自动开启和关闭

    public WebDriver webDriver;
    @BeforeClass
    public void setUpEnv(){
        EdgeOptions edgeOptions = new EdgeOptions();
        edgeOptions.addArguments("--remote-allow-origins=*");

        Path p1 = Paths.get("src","drivers","msedgedriver.exe");//配置路径
        System.setProperty("webdriver.edge.driver",p1.toAbsolutePath().toString());//根据路径引入依赖
        webDriver = new EdgeDriver(edgeOptions);
    }
    @AfterClass
    public void tearDownEnv(){
        webDriver.quit();//退出浏览器
    }

测试打开百度网页

    @Test
    public void openBaidu(){

        webDriver.get("https://www.baidu.com");
    }

完整代码

package com.abtester;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author Gds
 * @date 2023/11/15$ 18:29$
 * @description:
 */
public class TestCase {
    public WebDriver webDriver;
    @BeforeClass
    public void setUpEnv(){
        EdgeOptions edgeOptions = new EdgeOptions();
        edgeOptions.addArguments("--remote-allow-origins=*");

        Path p1 = Paths.get("src","drivers","msedgedriver.exe");
        System.setProperty("webdriver.edge.driver",p1.toAbsolutePath().toString());
        webDriver = new EdgeDriver(edgeOptions);
    }
    @Test
    public void openBaidu(){

        webDriver.get("https://www.baidu.com");
    }
    @AfterClass
    public void tearDownEnv(){
        webDriver.quit();
    }
}

Selenium-Java_day_01—②测试百度搜索HelloWorld

代码演示

    @Test
    public void openBaidu() throws InterruptedException {
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.id("kw")).sendKeys("HelloWorld");//查找搜索框id并且输入内容
        webDriver.findElement(By.id("su")).click();//提交/点击操作
        Thread.sleep(3_000);//延迟时间注意需要加上异常处理才可以
    }

Selenium-Java_day_01—③通过Class和Name定位

代码演示

    @Test
    public void testName() throws InterruptedException {
        webDriver.findElement(By.name("wd")).sendKeys("新疆师范大学官网");//通过name属性去查找
        webDriver.findElement(By.className("s_btn")).click();//也可以通过class属性去查找,class属性查找前提是s_btn是唯一值
        Thread.sleep(3_000);//设置延迟时间
    }

Selenium-Java_day_01—③通过XPATH和CSS定位

XPATH定位

在这里插入图片描述

在这里插入图片描述
这里讲解一点,现在的edge浏览器都可以自己获取Xpath路径,不需要自己手写,F12定位到元素以后右键复制下拉栏有一个复制Xpath路径。

    @Test
    public void testXpath() throws InterruptedException {
        webDriver.findElement(By.xpath("//a[text()=\"新华网\"]")).click();
        Thread.sleep(3_000);//设置延迟时间
    }

在这里插入图片描述

CSS定位

在这里插入图片描述

​ 上述方法利用了CSS定位里面的相对属性来定位来实现具体操作内容

Selenium-Java_day_02—①actions模拟鼠标移动

找到更多连接方式

在这里插入图片描述

根据需要去查看需要的内容

    @Test
    public void testActions() throws InterruptedException {
        Actions actions = new Actions(webDriver);//设置动作移动
        WebElement moreBtn =  webDriver.findElement(By.name("tj_briicon"));//设置移动到name上并且新建动作
        actions.moveToElement(moreBtn);//设置移动到此为止上面
        WebElement liveBtn =webDriver.findElement(By.xpath("//a[@name=\"tj_live\"]"));//通过Xpath语句查找到下一个动作链接地址
        actions.moveToElement(liveBtn).click().perform();//设置指向此位置的动作并且点击

    }

Selenium-Java_day_02—②百度地图拖拽功能测试

测试代码

    @Test
    public void testGraph() throws InterruptedException {
        webDriver.get("https://www.amap.com/");
        WebElement mask = webDriver.findElement(By.xpath("//*[@id=\"renderArrowLayer\"]"));
        Thread.sleep(10_000);//设置延迟时间
        Actions actions = new Actions(webDriver);
        actions.moveToElement(mask).contextClick().pause(5000);//右击并且停留三秒
        WebElement cmitem_start = webDriver.findElement(By.xpath("//*[@id=\"menuFrom\"]"));//通过xpath语句查找设为起点的属性
        actions.moveToElement(cmitem_start).click();//进行单击操作
        actions.clickAndHold();//按住拖动不放手
        actions.moveByOffset(200,100);//设置唯一路径X轴和Y轴、
        actions.release();//松开鼠标
        actions.contextClick();//再次移动
        actions.pause(3000);
        WebElement cmitem_end = webDriver.findElement(By.id("menuTo"));//通过xpath语句查找设为终点的属性
        actions.moveToElement(cmitem_end).click();
        actions.perform();//将以上构建的项目全部执行
    }

Selenium-Java_day_02—③浏览器窗口切换功能测试

测试代码

    @Test
    public void testSwitchWindows() throws InterruptedException {
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.linkText("贴吧")).click();
        String currentWindows =  webDriver.getWindowHandle();//获取当前浏览器窗口

        Set<String> allWindows = webDriver.getWindowHandles();//获得所有浏览器窗口
        for (String window:allWindows){
            if(window != currentWindows){
                webDriver.switchTo().window(window);
            }
        }//设置切换类型
        webDriver.findElement(By.id("wd1")).sendKeys("孙笑川吧");
        Thread.sleep(3_000);//设置延迟时间
        webDriver.switchTo().window(currentWindows);
        webDriver.findElement(By.id("kw")).sendKeys("hahahahah");
    }

Selenium-Java_day_02—④iframe切换功能测试

测试代码(安居客)

    @Test
    public void testSwitchIframe(){//安居客爬虫测试
        webDriver.get("https://login.anjuke.com/login/form?history=aHR0cHM6Ly93dWx1bXVxaS5hbmp1a2UuY29tLw==");
        WebElement iframe = webDriver.findElement(By.id("iframeLoginIfm"));
        webDriver.switchTo().frame(iframe);
        webDriver.findElement(By.xpath("//*[@id=\"checkagree\"]")).click();
        webDriver.findElement(By.id("phoneIpt")).sendKeys("11111111111");
        webDriver.findElement(By.id("sendSmsBtn")).click();
        
    }

以上代码有时效性,仅提供学习思路,因为随着浏览器的更新和时间的推移,插件可能也有所更新,其实edge和Chrome都差不多,只是拉取驱动的代码略有小小的差异。以上涉及的代码和网址仅供学习参考使用,切勿用来做违法违纪之事。
微软浏览器插件下载官方网址:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/#downloads

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值