前言

使用selenium进行淘宝购物测试


一、登录滑块验证

直接说结论:别用密码了,直接手动扫码登录
淘宝检测到selenium后你就没办法通过滑块验证了
要想绕过selenium就是反反爬虫的内容了,如果只是初次使用selenium完成案例就用我这种办法吧。

二、搜索

淘宝首页的搜索框旁边那个不是下拉框selector

三、跳转新窗口

使用switchTo函数跳转,下面源代码里有,跳转代码可以集成一个函数,我没集成,所以有很多代码复用,有时间的可以改改再用

package taobao;

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Purchase {
	public static void main(String[] args) {
		WebDriver driver;
		System.setProperty("webdriver.geckodriver.driver",
				"D:\\Software\\eclipse-jee-2021-09-R-win32-x86_64\\eclipse\\geckodriver.exe");
//		System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
		ChromeOptions option = new ChromeOptions();
		option.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
		driver = new ChromeDriver(option);
		driver.get("https://www.taobao.com/");
		String main_handle = driver.getWindowHandle();
		driver.findElement(By.xpath("//*[@id=\"J_SiteNavLogin\"]/div[1]/div[1]/a[1]")).click();
		driver.findElement(By.xpath("//*[@id=\"login\"]/div[1]/i")).click();
//		为手动扫码登录预留时间
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.findElement(By.xpath("//*[@id=\"J_Search\"]/div/ul/li[1]")).click();
		driver.findElement(By.xpath("//*[@id=\"J_Search\"]/div/ul/li[3]")).click();
		driver.findElement(By.xpath("//*[@id=\"q\"]")).sendKeys("迪卡侬旗舰店");
		driver.findElement(By.xpath("//*[@id=\"J_TSearchForm\"]/div[1]/button")).click();
		driver.findElement(By.xpath("//*[@id=\"list-container\"]/li[1]/ul/li[1]/a/img")).click();
//		切换控制窗口到旗舰店首页
		Set<String> handles = driver.getWindowHandles();
		for (String handle : handles) {
			if (handle.equals(main_handle) == false) {
				driver.switchTo().window(handle);
			}
		}
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 旗舰店主页句柄
		String dian_handle = driver.getWindowHandle();
		// 点击“男士”标签
		driver.findElement(By.xpath("//*[@id=\"shop21980756097\"]/div/div[2]/div/div/div/div[2]/a[2]/div/div[1]"))
				.click();
//		切换控制窗口到旗舰店首页
		handles = driver.getWindowHandles();
		for (String handle : handles) {
			if ((handle.equals(main_handle) == false) && (handle.equals(dian_handle) == false)) {
				driver.switchTo().window(handle);
			}
		}
		// 男士商品列表界面句柄
		String list_handle = driver.getWindowHandle();

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 第一件商品
		driver.findElement(By.xpath("//*[@id=\"J_ShopSearchResult\"]/div/div[3]/div[1]/dl[1]/dt/a/img")).click();
//		String g1_handle = driver.getWindowHandle();
//		切换控制窗口到商品详细页面
		handles = driver.getWindowHandles();
		for (String handle : handles) {
			if ((handle.equals(main_handle) == false) && (handle.equals(dian_handle) == false)
					&& (handle.equals(list_handle) == false)) {
				driver.switchTo().window(handle);
			}
		}
		driver.findElement(By.xpath("//*[@id=\"J_DetailMeta\"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[1]/a"))
				.click();
		driver.findElement(
				By.xpath("//*[@id=\"J_DetailMeta\"]/div[1]/div[1]/div/div[4]/div/div/dl[2]/dd/ul/li[1]/a/span"))
				.click();
		driver.findElement(By.xpath("//*[@id=\"J_LinkBasket\"]")).click();

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		driver.switchTo().window(list_handle);

		// 第二件商品
		driver.findElement(By.xpath("//*[@id=\"J_ShopSearchResult\"]/div/div[3]/div[1]/dl[2]/dt/a/img")).click();
		handles = driver.getWindowHandles();
		for (String handle : handles) {
			if ((handle.equals(main_handle) == false) && (handle.equals(dian_handle) == false)
					&& (handle.equals(list_handle) == false)) {
				driver.switchTo().window(handle);
			}
		}
		driver.findElement(By.xpath("//*[@id=\"J_DetailMeta\"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[1]/a"))
				.click();
		driver.findElement(
				By.xpath("//*[@id=\"J_DetailMeta\"]/div[1]/div[1]/div/div[4]/div/div/dl[2]/dd/ul/li[1]/a/span"))
				.click();
		driver.findElement(By.xpath("//*[@id=\"J_LinkBasket\"]")).click();

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 进入购物车zjw
		driver.findElement(By.xpath("//*[@id=\"sn-bd\"]/div/ul/li[2]/a")).click();
//		切换控制窗口到购物车页面
		handles = driver.getWindowHandles();
		for (String handle : handles) {
			if ((handle.equals(main_handle) == false) && (handle.equals(dian_handle) == false)
					&& (handle.equals(list_handle) == false)) {
				driver.switchTo().window(handle);
			}
		}
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
//		全选
		driver.findElement(By.xpath("//*[@id=\"J_SelectAll1\"]/div/label")).click();
//		删除
		driver.findElement(By.xpath("//*[@id=\"J_FloatBar\"]/div[2]/div[2]/a[1]")).click();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
//		点击确认
		driver.findElement(By.className("J_DialogConfirmBtn")).click();

		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.quit();
	}
}

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐