java+selenium的入门学习记录

有时候需要浏览器做重复的操作,比如程序员WebUi测试,或者领导给你一个网站和一堆账号,要求你完成答题问卷等。Jsoup和java网络编程等爬虫方法只能爬取html源,不执行javascript。所以使用火狐浏览器的插件iMacros,唯一缺点是无法点击javascript动态生成的界面(原理是记录点击步骤时同时记录所点击的码源html标签,遇到javascript动态生成的界面时操作结束),java可以Htmlunit和Selenium,区别是Htmlunit是无界面模拟,存在一部分javascript执行失效。Selenium是真实浏览器模拟,克服一些javascript执行出现的问题。

参照https://blog.csdn.net/qq_29073921/article/details/83714299

1、maven引入selenium 的jar依赖

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.11.1</version>
</dependency>
   <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>22.0</version>
    </dependency>

2、google浏览器驱动下载并复制到google安装文件位置文件夹(先查找浏览器对应驱动版本,否侧必定出错,zhanghong踩过坑)

2019 Selenium Chrome版本与chromedriver兼容版本对照表

3、亲测代码

        //加载驱动,后面的路径自己要选择正确,也可以放在本地
                System.setProperty("webdriver.chrome.driver", "C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chromedriver.exe");
                
                //初始化一个谷歌浏览器实例,实例名称叫driver
                WebDriver driver = new ChromeDriver();
                // get()打开一个站点
                driver.get("https://www.baidu.com");
                Thread.sleep(1000);
                //获取输入框元素,并输入值
                driver.findElement(By.id("kw")).sendKeys("我爱学习");
              //获取搜索元素,并点击
                driver.findElement(By.id("su")).click();
        
        System.out.println("+++++++++++");

4、要用到的一下技术xpath标签定位(如driver.findElement(By.xpath("//*[@class='login-btn']")).click();)

查找页面中class=login-btn的元素并点击

5、本次学习完美解决  不能解决的javascript动态生成的alert对话框。

1 driver.findElement(By.id("updateButton")).click();
2 //pop up with id "updateButton" opens
3 Alert alert = driver.switchTo().alert();
4 //update is executed
5 alert.accept();

如果是点取消,则:

alert.dismiss();

Selenium对alert的处理https://www.cnblogs.com/qiaoyeye/p/5593428.html

6、元素定位相关学习https://www.cnblogs.com/Nancy-Lee/p/11011353.html

https://blog.csdn.net/qq_22003641/article/details/79137327

https://blog.csdn.net/q1694222672/article/details/82836315

7、对于常用元素定位及隐藏元素(鼠标悬停等才会出现的内容如:退出,下拉菜单等)的操作

  1. // Login 功能 多种定位方式------------

  2. // driver.findElement(By.linkText("登录")).click();连接文本方式

  3. //driver.findElement(By.partialLinkText("首页")).click(); 模糊连接文本方式

  4. // driver.findElement(By.id("loginname")).click();id方式

  5. // driver.findElement(By.className("ef")).click();class方式

  6. // driver.findElement(By.name("loginname")).click();name方式

  7. // driver.findElement(By.tagName("input")).sendKeys("13437868119");tag

  8. // driver.findElement(By.xpath("//*[@id=\"loginname\"]")).sendKeys("13437868119");Xpath 定位

  9. driver.findElement(By.cssSelector("#loginname")).sendKeys("13437868119");//填写文本框

隐藏元素先鼠标悬停,显示出元素在定位元素并操作

                    Actions actions = new Actions(driver);
                    //鼠标悬停
                    actions.moveToElement(driver.findElement(By.className("down-triangle"))).perform();
                    Thread.sleep(1000);
                    //driver.findElement(By.className("down-triangle")).click();//鼠标悬停
                    driver.findElement(By.id("logout")).click();//鼠标悬停后在查找并操作

 

我的测试代码:

package MavenTest.MavenTest;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws InterruptedException
    {
    	//加载驱动,后面的路径自己要选择正确,也可以放在本地
    			System.setProperty("webdriver.chrome.driver", "C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chromedriver.exe");
    			
    			//初始化一个谷歌浏览器实例,实例名称叫driver
    			WebDriver driver = new ChromeDriver();
    			driver.manage().window().maximize();    // 最大化页面
    			driver.manage().deleteAllCookies();
    			// 与浏览器同步非常重要,必须等待浏览器加载完毕
    			driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    		    try {
    		    	// get()打开一个站点
        		    driver.get("https://static.qspfw.com/xf2020/learn_practice_list.html");
        		    Thread.sleep(1000);
        		    
        		    driver.findElement(By.id("toLogin")).click();
        		    Thread.sleep(1000);
        		    
        		    //获取输入框元素,并输入值
        		    driver.findElement(By.id("username")).sendKeys("s170692111223535659");
        		    driver.findElement(By.id("password")).sendKeys("123456");
        		   //获取搜索元素,并点击login-btn
        		    driver.findElement(By.xpath("//*[@class='login-btn']")).click();
        		    Thread.sleep(1000);
        		    //点击alert对话框
        		    Alert alert = driver.switchTo().alert();
        		    alert.accept();
        		    System.out.println("主页加载中。。。");
        		    WebDriverWait w=new WebDriverWait(driver,50,1);//创建一个等待,最大等待30秒,每秒检测一次
        	        w.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("/html/body/section/div[2]/div[1]/div[1]/a/div[2]")));//任意检测一个页面元素,看页面是否价值完成
        		    System.out.println("主页加载完毕");
        		    driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[3]")).click();
        		    Thread.sleep(1000);
        		    driver.findElement(By.xpath("//*[@id=\"columnList\"]/div[2]/div/div[2]/div[3]/div[1]")).click();
        		    Thread.sleep(1000);
        		    driver.findElement(By.id("afterClassPractice")).click();
        		    Thread.sleep(1000);
        		    driver.findElement(By.id("next_exam")).click();
        		    Thread.sleep(1000);
        		    /*
        		    Actions actions = new Actions(driver);//鼠标悬停
        		    actions.moveToElement(driver.findElement(By.className("down-triangle"))).perform();
        		    Thread.sleep(1000);
        		    driver.findElement(By.id("logout")).click();//logout  
        		    */
        		    Thread.sleep(1000);
        		    System.out.println("执行完毕le!");
        		    //driver.close();//关闭浏览器
    		    }
    		    catch(InterruptedException e) {
    		    	System.out.println(e.getMessage());
    		    	System.out.println("********************************************************");
    		    	System.out.println(e.getLocalizedMessage());
    		    	//driver.close();//出错关闭浏览器
    		    }
    		    
    		    
	    /*测试
	  //初始化一个谷歌浏览器实例,实例名称叫driver
		WebDriver driver = new ChromeDriver();
		// get()打开一个站点
	    driver.get("https://www.baidu.com");
	    Thread.sleep(1000);
	    //获取输入框元素,并输入值
	    driver.findElement(By.id("kw")).sendKeys("我爱学习");
	  //获取搜索元素,并点击
	    driver.findElement(By.id("su")).click();
	    */
    }
}

Maven如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>MavenTest</groupId>
  <artifactId>MavenTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.11.1</version>
</dependency>
   <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
	<dependency>
	    <groupId>com.google.guava</groupId>
	    <artifactId>guava</artifactId>
	    <version>22.0</version>
	</dependency>
    
</dependencies>
  


</project>


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值