Written in the book of the former
Most of you are familiar with crawlers. Generally speaking, using HttpClient to send a request and get a response to get the data you want to extract should be the most common method. Selenium has been used a lot in my work recently. In this article, we will use Selenium and POI (Reading and Writing Excel) to complete an entry-level automation program. See appendix for the source code.
Steps in
- Create projects using Maven, introducing Selenium and POI dependencies
- Download ChromeDriver and configure environment variables
- Write Selenium word search scripts
- Read and write Excel and save
- Write the main method, run the program
now
-
Create projects using Maven, introducing Selenium and POI dependencies
1.1 Download Maven and configure environment variables
For Windows and Mac, you can write the Maven directory address into the path. The detailed steps can be found in Baidu and Google.
1.2 Configuring Maven in IDEA
The Maven version of IDEA may not be the latest. You are advised to import the latest local version.
1.3 Creating a Project
To create the project, just use the most basic template, which is to click Next.
1.4 Search for Selenium,POI, and PoI-OOXML dependencies at mvNRepository.com, import them to Pom.xml, click Import Change in the lower right corner, and add the following to pom.xml:
<dependencies> <! -- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> < the groupId > org. Seleniumhq. Selenium < / groupId > < artifactId > selenium - Java < / artifactId > < version > 3.14.0 < / version > </dependency> <! -- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> The < artifactId > poi < / artifactId > < version > 4.0.0 < / version > < / dependency > <! -- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> The < artifactId > poi - ooxml < / artifactId > < version > 4.0.0 < / version > < / dependency > < / dependencies >Copy the code
-
Download ChromeDriver and configure environment variables (choose one of three)
2.1 Download ChromeDriver from the Mirror and configure environment variables
If you do not configure environment variables after manually downloading ChromeDriver, add system. setProperty(” webDriver.chrome. driver”,path) to the code. Where path is your driver path.
Choco Install for Windows
2.3 Brew Install Cask for Mac
-
Write Selenium word search scripts
In setUp, WebDriver needs to be initialized first, then visit youdao home page, Search test, click OK and jump to the Search page. Notice that an advertisement will pop up when driver visits this page, and a line of code is needed to grab the close link and close the advertisement. The code is as follows:
//Direct to YoudaoDic homepage, land in the main search page public void setUp(a) { //Go to youdao.com driver.get(YOUDAO_HOME_URL); driver.manage().window().maximize(); //Go to the main search page driver.findElement(By.id(INPUT_HOME_ID)).sendKeys("test"); driver.findElement(By.xpath(SEARCH_HOME_XPATH)).click(); driver.findElement(By.xpath(CLOSE_BTN)).click(); } Copy the code
3.2 Writing searchWord script method
The searchWord method requires passing in the word you want to search for, grabbing the search box, typing it, and clicking OK. Then you will get the search details page, where you need to grab the Chinese translation of div and get the text, the code is as follows:
//Search word and get the translation public String searchword(String s) { //Find the input element, input the word and click the button WebElement input_search = driver.findElement(By.id(INPUT_SEARCH)); input_search.clear(); input_search.sendKeys(s); driver.findElement(By.xpath(SEARCH_BTN_XPATH)).click(); //Get the text inside translation div String result = driver.findElement(By.className(TRANSLATION_CLASS)).getText(); return result; } Copy the code
-
Read and write Excel and save
4.1 Create Excel files and write words
Create a new Excel and fill in some words in the first leftmost column. Be careful not to have empty lines. There is no exception handling in this code.
4.2 Write Excelio class, write read method
Using the POI framework, and ordinary file reading and writing similar, the code is as follows:
public Workbook read(int columnIndex, int count) throws IOException { FileInputStream fis = null; fis = new FileInputStream(new File(path)); //Input and save as a xlsx workbook Workbook workbook = new XSSFWorkbook(fis); fis.close(); return workbook; } Copy the code
4.3 Writing searchWord method
Call searchWord of the Search class to Search, and then write the obtained String to Excel with the following code:
//Search the word and write down public void searchWord(Workbook workbook, int columnIndex, int count) { //Initialize driver WebDriver driver = new ChromeDriver(); Search search = new Search(driver); search.setUp(); //Search for all words in one column and print to another column for (int i = 0; i < count; i++) { //Get value of the cell and get the translation through search method Sheet sheet = workbook.getSheetAt(0); Row row = sheet.getRow(i); Cell cell = row.getCell(columnIndex); String results = search.searchword(cell.getStringCellValue()); //Write the translation to another column Cell temp = row.createCell(columnIndex+1); temp.setCellValue(results); //Set the new column as "Wrap Text" CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(true); temp.setCellStyle(cellStyle); sheet.setColumnWidth(1.31*256); } Copy the code
4.4 Write the save method
Using FileOutputStream, save Excel with the following code:
//Save the change public void save(Workbook workbook) throws IOException { FileOutputStream outputStream = new FileOutputStream(path); workbook.write(outputStream); outputStream.close(); workbook.close(); } Copy the code
-
Write the main method, run the program
Write the entry method as follows:
//Entrance
public static void main(String[] args) throws IOException {
Excelio excelio = new Excelio("src/main/resources/wordlist/test.xlsx");
Workbook workbook = excelio.read(0.30);
excelio.searchWord(workbook,0.30);
excelio.save(workbook);
}
Copy the code
The results show
Excel
conclusion
This article is designed as an introduction to Selenium, and subsequent authors will update their articles on Automated WebApp based on Selenium and JavaWeb. Stay tuned. If you have any questions, welcome to leave a message, welcome to issue and PR, thank you for reading.
The appendix
Source code address: gitee.com/daniel_ddd/…