selenium
java selenium自动化项目实战
参考资料:
java+selenium元素定位和元素操作
1.【报错】org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element … is not clickable at point (1458, 409). Other element would receive the click:
解决方案:
第一种方式若报错,可以使用二三方式:
- 普通方式
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div[1]/div[2]/button")).click(); //普通方式
- 使用键盘回车的方式
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div[1]/div[2]/button")).click(); //使用键盘回车键的方式
- 使用鼠标点击的方式
WebElement element = webDriver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div[1]/div[2]/button"));
(new Actions(webDriver)).moveToElement(element).click().perform();
在爬取携程买票界面,使用第二种方式使用键盘回车点击搜索有效。
2. 携程购买火车票实战操作
先下载驱动chromedriver.exe,放在google浏览器所在的位置,复制路径放在下边
测试一下在【携程】购买国内火车票
@SpringBootTest
public class CtripTest {
private static final String HRWORK_LOGIN = "https://trains.ctrip.com/"; //购买火车票
private ThreadLocal<WebDriver> webDriverThreadLocal = new ThreadLocal<>();
private void init(){
// 加载chromedriver 是使用chorme的必要条件 google浏览器驱动
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\Google\\Chrome\\Application\\driver\\chromedriver.exe");
//System.setProperty("webdriver.chrome.driver",path);
//储存在系统变量中,变量名为webdriver.chrome.driver,值为D:\guge\ChromeCore\chromedriver.exe
// 添加chrome的配置信息
ChromeOptions chromeOptions = new ChromeOptions();
// 设置为无头模式
//chromeOptions.addArguments("--headless");
// 设置打开的窗口大小 非必要属性
//chromeOptions.addArguments("--window-size=500,500");
chromeOptions.addArguments("--start-maximized"); //最大化
// 使用配置信息 创建driver对象
RemoteWebDriver driver = new ChromeDriver(chromeOptions);
webDriverThreadLocal.set(driver);
}
@Test
public void maipiao() throws InterruptedException{
init();
WebDriver webDriver = webDriverThreadLocal.get();
webDriver.get(HRWORK_LOGIN); //进入买票界面
Thread.sleep(1000);
webDriver.findElement(By.id("label-departStation")).clear(); //清空出发城市输入框
webDriver.findElement(By.id("label-departStation")).sendKeys("周口");
webDriver.findElement(By.id("label-arriveStation")).clear(); //清空目的城市输入框
webDriver.findElement(By.id("label-arriveStation")).sendKeys("苏州");
Thread.sleep(1000);
webDriver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div[1]/div[2]/button")).sendKeys(Keys.ENTER); //使用键盘回车键的方式
Thread.sleep(5000);
webDriver.findElement(By.xpath("//*[@id=\"trainlistitem0\"]/div/button")).click(); //点击订票
Thread.sleep(1000);
webDriver.findElement(By.xpath("//*[@id=\"__next\"]/div/div[3]/div[1]/section/div[2]/ul/li[1]/button")).click(); //硬座
Thread.sleep(3000); //等待几秒钟,,页面加载比较慢
webDriver.findElement(By.id("nloginname")).sendKeys("手机号");
webDriver.findElement(By.id("npwd")).sendKeys("密码");
Thread.sleep(1000);
webDriver.findElement(By.xpath("//*[@id=\"normalview\"]/p/input")).click(); //已阅读
webDriver.findElement(By.id("nsubmit")).click(); //登录
Thread.sleep(1000);
webDriver.findElement(By.xpath("//*[@id=\"__next\"]/div/div[3]/div[1]/div[3]/div[2]/button")).click(); //添加乘客
Thread.sleep(1000);
webDriver.findElement(By.xpath("/html/body/div[10]/div[1]/div[2]/div/div/div[2]/div/button")).click(); //新增乘客
Thread.sleep(1000);
webDriver.findElement(By.xpath("/html/body/div[11]/div[1]/div[2]/div/div/div[2]/ul/li[2]/div[2]/input")).sendKeys("郭靓仔"); //姓名
Thread.sleep(1000);
webDriver.findElement(By.xpath("/html/body/div[11]/div[1]/div[2]/div/div/div[2]/ul/li[4]/div[2]/input")).sendKeys("41270505565****"); //证件号码
Thread.sleep(1000);
webDriver.findElement(By.xpath("/html/body/div[11]/div[1]/div[2]/div/div/div[2]/ul/li[5]/div[2]/input")).sendKeys("41270505565****"); //手机号
webDriver.findElement(By.xpath("/html/body/div[11]/div[1]/div[2]/div/div/div[3]/button[2]")).click(); //确定添加
}
}
上面主要实现了,元素定位以及元素操作
定位:
- ID定位元素:findElement(By.id(“”));
- 通过元素的html中的位置定位元素:findElement(By.xpath(“”));
操作:
- 单击.click():普通单击,键盘回车单击,鼠标单击
- 输入.sendKeys():在输入框中输入内容(登录的账号密码,出发地,目的地等)
- 清空内容.clear():清空输入框中的内容
3. 滑块
@SpringBootTest
public class CtripTest {
private static final String HRWORK_LOGIN = "https://passport.ctrip.com/user/login?"; //登录
private ThreadLocal<WebDriver> webDriverThreadLocal = new ThreadLocal<>();
private void init(){
// 加载chromedriver 是使用chorme的必要条件 google浏览器驱动
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\Google\\Chrome\\Application\\driver\\chromedriver.exe");
//System.setProperty("webdriver.chrome.driver",path);
//储存在系统变量中,变量名为webdriver.chrome.driver,值为D:\guge\ChromeCore\chromedriver.exe
// 添加chrome的配置信息
ChromeOptions chromeOptions = new ChromeOptions();
// 设置为无头模式
//chromeOptions.addArguments("--headless");
// 设置打开的窗口大小 非必要属性
//chromeOptions.addArguments("--window-size=500,500");
chromeOptions.addArguments("--start-maximized"); //最大化
// 使用配置信息 创建driver对象
RemoteWebDriver driver = new ChromeDriver(chromeOptions);
webDriverThreadLocal.set(driver);
}
@Test
public void login() throws InterruptedException {
init();
WebDriver webDriver = webDriverThreadLocal.get();
webDriver.get(HRWORK_LOGIN);
Thread.sleep(1000); //等待几秒钟,,页面加载比较慢
webDriver.findElement(By.id("nsubmit")).click(); //登录 为了弹出滑块
webDriver.findElement(By.id("nloginname")).sendKeys("手机号");
webDriver.findElement(By.id("npwd")).sendKeys("密码");
Thread.sleep(1000);
webDriver.findElement(By.xpath("//*[@id=\"normalview\"]/p/input")).click(); //已阅读
webDriver.findElement(By.id("nsubmit")).click(); //登录
Thread.sleep(1000);
/**
* 滑块的移动
*/
WebElement sour = webDriver.findElement(By.xpath("//*[@id=\"sliderddnormal\"]/div/div[4]/div[3]/div/i[1]")); //滑块内部
WebElement ele = webDriver.findElement(By.cssSelector("#sliderddnormal > div > div.cpt-drop-box")); //整个滑道
Actions actions = new Actions(webDriver);
actions.dragAndDropBy(sour,ele.getSize().getWidth(),-ele.getSize().getHeight()).perform(); //要拖拽的滑块,x,y
//这个目前不能滑动到特定的位置,只能直接划到头
Thread.sleep(1000);
if (!webDriver.getCurrentUrl().contains("/home")) //登录成功检验
throw new RuntimeException("网页访问出错,请重试");
}
}
3.JS/Jquery
用Js/jQuery做一个自动测试
每一个星球都有一个驱动核心,
每一种思想都有影响力的种子。
感受世界的温度,
年轻的你也能成为改变世界的动力,
百度珍惜你所有的潜力。
你的潜力,是改变世界的动力!
package com.mask;
import org.junit.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class CtripTest01 {
@Test
public void Jstest01(){
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\Google\\Chrome\\Application\\driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
//js.executeScript("document.getElementById('kw').value='酸菜'"); //js
js.executeScript("$('#kw').val('酸菜')"); //jQuery
js.executeScript("document.getElementById('su').click()");
}
}