重点:
打开新的窗口后,driver发生了变化,不能再用之前的driver;可以通过窗口句柄,跳转到新页面
// 页面跳转,driver再次发生变化;(既 :重新打开一个浏览器窗口后,driver发生了变化,不能使用原先窗口的driver) for (String windowHandle : driver.getWindowHandles()) { handle = windowHandle; } //切换driver driver.switchTo().window(handle);
整体代码
package douyin; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.annotations.Test; import utils.ReadPropertiesUtils; import java.io.IOException; import java.util.List; public class PingLun06ToBlog { public static WebDriver driver; public static String content="您好,感谢您"; @Test public void test01() throws InterruptedException, IOException { System.out.println("1"); getDriver(); } public static WebDriver getDriver() throws InterruptedException, IOException { // Google chrome driver位置 String driverPath = "D:\\soft\\chrome120\\chrome.exe"; // chrome 浏览器位置 String browerPath = "D:\\file\\jar\\chromeDriver120\\chromedriver.exe"; // chrome 浏览器数据存储目录位置 String userData="--user-data-dir=C:\\Users\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\"; System.setProperty("webdriver.chrome.driver", driverPath); System.setProperty("webdriver.chrome.bin", browerPath); ChromeOptions chromeOptions = new ChromeOptions(); // 防止403 chromeOptions.addArguments("--remote-allow-origins=*"); chromeOptions.addArguments(userData); driver = new ChromeDriver(chromeOptions); String url = "https://www.douyin.com/user/self?showTab=like"; driver.get(url); String firstHandle = driver.getWindowHandle(); // 最大化窗口 driver.manage().window().maximize(); Thread.sleep(3000); // 加载需要发送的抖音号 List<String> listsKeys= ReadExcel.getListByText(); for (int i = 0; i < listsKeys.size(); i++) { // 抖音号 String key=listsKeys.get(i); System.out.println(i+ " = "+key+ " 开始发送"); Thread.sleep(500); //清除搜索框内容 driver.findElement(By.xpath("//*[@id=\"douyin-header\"]/div[1]/header/div/div/div[1]/div/div[2]/div/div/input")).sendKeys(Keys.CONTROL+"a"); Thread.sleep(500); // 搜索框输入内容 driver.findElement(By.xpath("//*[@id=\"douyin-header\"]/div[1]/header/div/div/div[1]/div/div[2]/div/div/input")).sendKeys(key); // 点击搜索 driver.findElement(By.xpath("//span[text()='搜索']")).click(); String handle = ""; // 用于装浏览器窗口句柄 // 页面跳转,driver再次发生变化;(既 :重新打开一个浏览器窗口后,driver发生了变化,不能使用原先窗口的driver) for (String windowHandle : driver.getWindowHandles()) { handle = windowHandle; } //切换driver driver.switchTo().window(handle); driver.get(driver.getCurrentUrl()); Thread.sleep(6000); //点击头像 List<WebElement> lists = driver.findElements(By.xpath("//ul//li[@class='MgWTwktU search-result-card mpsAEvY1']")); lists.get(0).findElement(By.xpath("//p[@class='_K3SLhVc']")).click(); Thread.sleep(500); // 页面跳转,driver再次发生变化 for (String windowHandle : driver.getWindowHandles()) { handle = windowHandle; } driver.switchTo().window(handle); driver.get(driver.getCurrentUrl()); Thread.sleep(6000); // 点击私信 driver.findElement(By.xpath("//button[text()='私信']")).click(); Thread.sleep(3000); // 发送消息 driver.findElement(By.xpath("//div[@class='public-DraftStyleDefault-block public-DraftStyleDefault-ltr']")).sendKeys(content); // 点击发送按钮 driver.findElement(By.xpath("//span[@class='cW9Hyt2D j5Gubm7H e2e-send-msg-btn']")).click(); Thread.sleep(3000); driver.switchTo().window(firstHandle); System.out.println(i+ " = "+key+ " 发送成功"); } return driver; } }
读取抖音id
package douyin; import java.io.*; import java.util.ArrayList; import java.util.List; public class ReadText { public static List<String> getListByText() throws IOException { List<String> lists = new ArrayList<>(); // String path="C:\\Users\\Desktop\\douyin\\douyin.txt"; File file= new File(path); BufferedReader br = new BufferedReader(new FileReader(file)); String str=""; while ((str = br.readLine())!=null){ lists.add(str); } return lists; } }
标签:私信,java,String,driver,selenium,sleep,import,div,public From: https://www.cnblogs.com/ychun/p/17894197.html