//package com.ruoyi.abuwx.controller;
//
//import org.openqa.selenium.By;
//import org.openqa.selenium.StaleElementReferenceException;
//import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebElement;
//import org.openqa.selenium.chrome.ChromeDriver;
//import org.openqa.selenium.chrome.ChromeOptions;
//import org.openqa.selenium.devtools.v85.network.Network;
//import org.openqa.selenium.interactions.Actions;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.support.ui.WebDriverWait;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.*;
//import java.util.concurrent.TimeUnit;
//
//public class WebDriverTestController {
//
//    private static final Logger log = LoggerFactory.getLogger(WebDriverTestController.class);
//
//    public static void main(String[] args) throws Exception {
//        // 设置 ChromeDriver 路径
//        System.setProperty("webdriver.chrome.driver", "D:\\chromeDriver\\chromedriver-win64\\chromedriver.exe");
//
//        // 创建 ChromeOptions 实例并设置 User-Agent
//        ChromeOptions options = new ChromeOptions();
//        options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
//
//        // 创建 ChromeDriver 并传入 ChromeOptions
//        WebDriver driver = new ChromeDriver(options);
//
//        //浏览器最大化
//        driver.manage().window().maximize();
//
//        // 访问目标网页
//        driver.get("https://www.baidu.com/");
//        WebElement location = driver.findElement(By.id("s-usersetting-top"));
//
//        Actions action = new Actions(driver);
//        action.moveToElement(location).perform();
//
//        driver.findElement(By.linkText("高级搜索")).click();
//
//        driver.findElement(By.id("adv_keyword")).clear();
//        driver.findElement(By.id("adv_keyword")).sendKeys("反诈");
//
////        driver.findElement(By.xpath("//file_ul_2a1K5/time_li_3_ArK pop_li_1YiIN pointer_u-S_a[1]")).click();
//
//        driver.findElement(By.id("q5_1")).click();
//        driver.findElement(By.name("q6")).sendKeys("news.163.com");
//
//        driver.findElement(By.xpath("//input[@value='高级搜索']")).click();
//
//
//        System.out.println(driver.getTitle());
//
//        String currentWindow = driver.getWindowHandle();
//        Set<String> windows = driver.getWindowHandles();
//
//        List<String> allWindows = new ArrayList<String>(windows);
//
//        driver.switchTo().window(allWindows.get(allWindows.size()-1));
//        driver.manage().window().maximize();
//
//        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//
//
//        driver.findElement(By.id("timeRlt")).click();
//        WebElement select = driver.findElement(By.xpath("//ul[@class='file_ul_2a1K5']"));
//        List<WebElement> tags = select.findElements(By.tagName("li"));
//        tags.forEach(o -> {
//            System.out.println(o);
//            if (Objects.equals(o.getText(), "一天内")) {
//                o.click();
//            }
//        });
//        Thread.sleep(1000);
//
//        WebDriverWait wait = new WebDriverWait(driver, 10);
//        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//h3/a")));
//        List<WebElement> titles = driver.findElements(By.xpath("//h3/a"));
//        if (titles.size() == 0) {
//            driver.quit();
//            return;
//        }
//        int random = new Random().nextInt(titles.size()-1);
//
//        WebElement title = titles.get(random);
//        String text = title.getText();
//        System.out.println(text);
//        try {
//            title.click();
//            allWindows=new ArrayList<>(driver.getWindowHandles());
//            driver.switchTo().window(allWindows.get(allWindows.size()-1));
//            System.out.println(driver.getTitle());
//
//            WebElement body=  driver.findElement(By.xpath("//div[@class='post_body']"));
//            String bodyText=body.getAttribute("innerText");
//            System.out.println(bodyText);
//            List<WebElement> paras = driver.findElements(By.xpath("//div[@class='post_body']//p"));
////            for (WebElement para : paras) {
////                String cont=  para.getAttribute("textContent");
////                System.out.println(cont+";class:"+para.getAttribute("class"));
////                if(Objects.equals(para.getAttribute("class"),"f_center")){
////                    System.out.println("这是图片");
//////                    String link =   wait.until(ExpectedConditions.visibilityOfElementLocated((By.cssSelector("img")))).getAttribute("src");
////                    WebElement img = para.findElement(By.cssSelector("img"));
////                    System.out.println("图片连接："+img.getAttribute("src"));
////                }
////            }
//        } catch (StaleElementReferenceException e) {
//            driver.quit();
//        }
//        finally {
//            driver.quit();
//        }
////        // 遍历并打印每个标题链接的文本和URL
////        for (WebElement link : titles) {
////             text = link.getText();
////            String url = link.getAttribute("href");
////            System.out.println("标题: " + text);
////            System.out.println("链接: " + url);
////            System.out.println("--------------------");
////        }
////        // 关闭浏览器
////        driver.quit();
//
//    }
//}
//