如何在谷歌网页的趋势列表中选择第二个


package jdjjddk;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Htegsbn {
public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C:\Users\hp\Downloads\Compressed\Chromedriver.exe");
WebDriver driver = new ChromeDriver(); 
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[@title='Search']")).click();
driver.findElement(By.xpath("//div[normalize-space()='Trending searches'][2]")).click();


}
}

这是我写的代码,执行时会停止显示趋势列表,但不会单击第二个。

Selenium中,您有FindElements

List<Element> elements = driver.findElements
(By.xpath("//div[normalize-space()='Trending searches']"));
elements[2].click();

最新更新