package com.example;// 注意:如果你使用手动设置路径,就不需要导入 WebDriverManager 了 // import io.github.bonigarcia.wdm.WebDriverManager; 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 org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.TimeoutException; import java.time.Duration; import java.util.List; public class test { // 更改类名以示区别 public static void main(String[] args) { // 手动设置 ChromeDriver 的路径 (如果你选择手动方式的话) // *** 将这里的路径替换为你实际的 chromedriver.exe 路径 *** System.setProperty("webdriver.chrome.driver", "F:\\tool\\EasySpider_0.6.2_Windows_x64\\EasySpider_windows_x64\\EasySpider\\resources\\app\\chrome_win64\\chromedriver_win64.exe"); // 如果你选择使用 WebDriverManager,则使用以下代码替代上面的 System.setProperty: // import io.github.bonigarcia.wdm.WebDriverManager; // WebDriverManager.chromedriver().setup(); WebDriver driver = null; try { // 配置 Chrome 选项 (可选) ChromeOptions options = new ChromeOptions(); // options.addArguments("--headless"); // 启用无头模式 // options.addArguments("--disable-gpu"); // 初始化 WebDriver driver = new ChromeDriver(options); // 直接打开包含搜索条件的 URL // 注意这里使用的 URL 已经包含了查询参数 driver.get("https://patentscope.wipo.int/search/en/result.jsf?query=FP:(AI)"); // 设置一个显式等待 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20)); // 由于直接打开了结果页,我们不再需要等待搜索框和点击按钮 // 直接等待搜索结果列表加载 // *** 请使用浏览器开发者工具确认这里的元素定位器是否正确 *** // "div.ps-result-list" 是一个可能的 CSS 选择器示例,你需要根据实际页面确认 wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.ps-result-list"))); // --- 在这里添加提取搜索结果的代码 --- // *** 请使用浏览器开发者工具确认这里的元素定位器是否正确 *** List resultItems = driver.findElements(By.cssSelector("div.ps-result-item")); // 定位每个结果项 System.out.println("Found " + resultItems.size() + " results:"); for (WebElement resultItem : resultItems) { try { // 提取标题 (示例选择器) // *** 请使用浏览器开发者工具确认这里的元素定位器是否正确 *** WebElement titleElement = resultItem.findElement(By.cssSelector("span.ps-field-value.ps-field-title")); String title = titleElement.getText().trim(); // 提取链接 (示例选择器) // *** 请使用浏览器开发者工具确认这里的元素定位器是否正确 *** WebElement linkElement = resultItem.findElement(By.tagName("a")); String patentLink = linkElement.getAttribute("href"); System.out.println("Title: " + title + ", Link: " + patentLink); } catch (NoSuchElementException e) { System.out.println("Could not find title or link for a result item in this result item."); continue; } } // --- 处理分页(如果需要)--- // 这部分逻辑与之前相同,你需要找到下一页按钮的定位器并实现循环点击和等待 // 尽管是直接打开结果页,如果结果有多页,你仍然需要处理分页来获取所有结果。 // ... } catch (TimeoutException e) { System.err.println("等待元素超时,可能页面结构发生变化或加载缓慢: " + e.getMessage()); } catch (NoSuchElementException e) { System.err.println("未能找到指定的元素,请检查元素定位器是否正确: " + e.getMessage()); } catch (Exception e) { System.err.println("发生其他错误: " + e.getMessage()); e.printStackTrace(); } finally { // 关闭浏览器 if (driver != null) { driver.quit(); System.out.println("Browser closed."); } } } }