findElements is used to find a set of elements, and findElement is used to find the first element of match expression.
Print For example here at Baidu news headlines with a module.
package rjcs; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Set; import org.openqa.selenium.interactions.Actions; import org.apache.commons.io.FileUtils; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.testng.annotations.Test; import org.openqa.selenium.*; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.openqa.selenium.OutputType; import com.thoughtworks.selenium.SeleneseTestNgHelper; public class ckqhh { public static void main(String[] args) { System.setProperty ("webdriver.firefox.bin", "C: \\ Program Files (the x86) \\ \\ firefox.exe the Mozilla Firefox"); // set the installation path Firefox, preventing system can not find FirefoxDriver Driver = new new FirefoxDriver () ; // initializes FireFox browser instance, and open the browser the try { driver.manage () window () maximize ();.. @ maximized window the Thread.sleep ( 3000 ); . driver.manage () window () .maximize (); // maximize the window Thread.sleep ( 3000 ); driver.get ( "https://news.baidu.com" ); List <WebElement> links = driver.findElements(By.xpath(".//*[@id='pane-news']/ul[1]/li/a")); for(int i=0; i<links.size(); i++) { System.out.println(links.get(i).getText()); } Thread.sleep(8000); }catch (Exception e) { e.printStackTrace(); }finally { driver.quit(); } } }
Results of the:
2020 people's livelihood, the central ministries such force!
NDRC: the Midwest and Northeast China city settled cancel basic restrictions
Xining bus station collapse continued: 16 injured nine people hospitalized and discharged
Spring Festival Transportation to send visitors has exceeded 100 million people: 12 million today is expected to send
the new version to the plastic limit a take-away, courier can not just use a plastic bag
again 20,000 tons! 4th year running the central reserve of frozen pork