Time: 2024-09-23 Monday 20:23:01
Author: Jackasher
软件自动化测试
本来不想做了,结果最后成功了,版本一直是对的,只是chrome有跨域问题

尽管如此还是有报错,但是这不影响运行 !!!只要接着往下写,就可以运行
下面是源码,注意打开新窗口是要切换到新窗口选取元素,同时今天知道了浏览器可以获取Xpath和Selector,还有JS路径,太方便了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 package org.example;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import java.util.Set;import java.util.concurrent.TimeUnit;public class BaiduSearch { private static final String URL = "https://shop.aircheng.com/" ; public static void test () throws InterruptedException { System.setProperty("webdriver.chrome.driver" , "/Users/leojackasher/Download/Compressed/chromedriver-mac-arm64 3/chromedriver" ); ChromeOptions options = new ChromeOptions (); options.addArguments("--remote-allow-origins=*" ); options.setBinary("/Users/leojackasher/Download/Compressed/chrome-mac-arm64 2/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing" ); WebDriver driver = new ChromeDriver (options); driver.get(URL); driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS); WebElement firstElement = driver.findElement(By.xpath("/html/body/header/div[2]/div/div/div[1]/ul/li[1]/div[1]/a[1]" )); firstElement.click(); Thread.sleep(2000 ); WebElement secondElement = driver.findElement(By.xpath("/html/body/div[3]/div/section[2]/section/section/ul/li[1]/a" )); secondElement.click(); Thread.sleep(2000 ); String originalWindow = driver.getWindowHandle(); Set<String> handles = driver.getWindowHandles(); for (String handle : handles) { if (!handle.equals(originalWindow)) { driver.switchTo().window(handle); break ; } } WebElement thirdElement = driver.findElement(By.xpath("/html/body/div[3]/div/section[2]/section[2]/div[2]/div[1]/span" )); thirdElement.click(); Thread.sleep(2000 ); driver.close(); driver.switchTo().window(originalWindow); driver.quit(); } public static void main (String[] args) throws InterruptedException { test(); } }