基于Selenium的網(wǎng)頁爬蟲2~3
一、實驗目的
1.通過動手實際操作,鞏固所學的系統(tǒng)功能測試相關知識;
2.初步了解Selenium工具的使用方法,加深對系統(tǒng)功能測試的認識。
3.熟練掌握利用Java語言,進行Selenium IDE和Selenium WebDriver的使用
二、實驗前提
1.學習系統(tǒng)測試基本知識;
2.熟悉Eclipse工具的基本操作;
3.掌握基于Eclipse工具的Java編程;
三、實驗內容
1.爬取新浪網(wǎng)中,新聞標簽的所有分類,并按分類將名字打印出來。提交腳本代碼和結果截圖。
2.爬取石河子大學首頁的所有機構信息,將名稱打印出來(要求至少用兩種不同的定位方法)。提交腳本代碼和結果截圖。
3.?爬取58同城二手房中,石河子地區(qū)“一小八中”的二手房信息,將房屋面積、房屋總價2個信息打印出來。提交腳本代碼和結果截圖。
4.以上3個實驗內容,完成其中1個即可。
四、實驗過程和結果
1.使用 selsniumide編輯腳本
?
圖1 使用 selsniumide編輯腳本
?
?
2.?使用eclipse 編輯腳本 代碼:
package com.test;
?
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
?
public class HttpswwwsinacomcnTest {
File outfile = new File("D:\\20201008368\\java_p_test2\\20201008368.txt");
??private WebDriver driver;
??private Map<String, Object> vars;
??JavascriptExecutor js;
??@Before
??public void setUp() {
System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver0.29.exe");
????driver = new FirefoxDriver();
????js = (JavascriptExecutor) driver;
????vars = new HashMap<String, Object>();
??}
??@After
??public void tearDown() {
????driver.quit();
??}
??public String waitForWindow(int timeout) {
????try {
??????Thread.sleep(timeout);
????} catch (InterruptedException e) {
??????e.printStackTrace();
????}
????Set<String> whNow = driver.getWindowHandles();
????Set<String> whThen = (Set<String>) vars.get("window_handles");
????if (whNow.size() > whThen.size()) {
??????whNow.removeAll(whThen);
????}
????return whNow.iterator().next();
??}
??@Test
??public void httpswwwsinacomcn() throws Exception, UnsupportedEncodingException {
????driver.get("https://www.sina.com.cn/");
????driver.manage().window().setSize(new Dimension(937, 860));
????js.executeScript("window.scrollTo(0,0)");
????vars.put("window_handles", driver.getWindowHandles());
????PrintWriter out = new PrintWriter(outfile, "UTF-8");
????
????WebElement opentab;
????String elementTest;
????for(int i=1;i<=3;i++) {
???? for(int j=1;j<=5;j++) {
???? ?for(int k=1;k<=4;k++) {
???? ????????opentab=driver.findElement(By.cssSelector(".nav-mod-1:nth-child(" + j + ") > ul:nth-child(" + i + ") > li:nth-child(" + k + ") > a"));
???? ???? elementTest=opentab.getText();
???? ???? out.print(elementTest);
???? ???? out.print(" ");
???? ?}
???? }
???? out.println(" ");
????}
?
????
????
????out.close();
????driver.close();
??}
}
?
3.?實驗結果圖
?
圖2 實驗結果截圖
?
新聞 軍事 國內 國際 體育 NBA 英超 中超 博客 專欄 專題 理財 時尚 女性 育兒 投資 微博 城市 上海 高爾夫 ?
財經(jīng) 股票 基金 外匯 娛樂 明星 電影 電視 視頻 綜藝 VR 直播 教育 留學 高考 佛學 旅游 文化 彩票 English ?
科技 手機 探索 眾測 汽車 報價 買車 新車 房產(chǎn) 二手房 家居 收藏 圖片 讀書 黑貓 政務 游戲 手游 郵箱 更多 ?
四、實驗過程和結果
1.使用selenium ide 錄入腳本
?
圖1 使用selenium ide 錄入腳本
?
2.?代碼
package?com.test;
?
import?org.junit.Test;
import?org.junit.Before;
import?org.junit.After;
import?java.util.*;
import?java.io.File;
import?java.io.FileNotFoundException;
import?java.io.PrintWriter;
import?java.io.UnsupportedEncodingException;
?
import?org.openqa.selenium.By;
import?org.openqa.selenium.WebDriver;
import?org.openqa.selenium.firefox.FirefoxDriver;
import?org.openqa.selenium.Dimension;
import?org.openqa.selenium.WebElement;
import?org.openqa.selenium.interactions.Actions;
import?org.openqa.selenium.JavascriptExecutor;
?
?
public?class?XinwenTest {
File outfile?= new?File("D:\\Desktop\\軟件測試20201008380\\實驗2\\20201008380.txt");
private?WebDriver driver;
private?Map<String, Object> vars;
JavascriptExecutor js;
@Before
public?void?setUp() {
System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver0.29.exe");
driver?= new?FirefoxDriver();
js?= (JavascriptExecutor) driver;
vars?= new?HashMap<String, Object>();
}
@After
public?void?tearDown() {
driver.quit();
}
public?String waitForWindow(int?timeout) {
try?{
Thread.sleep(timeout);
} catch?(InterruptedException e) {
e.printStackTrace();
}
Set<String> whNow?= driver.getWindowHandles();
Set<String> whThen?= (Set<String>) vars.get("window_handles");
if?(whNow.size() > whThen.size()) {
whNow.removeAll(whThen);
}
return?whNow.iterator().next();
}
@Test
public?void?untitled() throws?Exception, UnsupportedEncodingException {
driver.get("https://cn.58.com/");
driver.manage().window().setSize(new?Dimension(1936, 1056));
driver.findElement(By.id("keyword")).click();
driver.findElement(By.id("keyword")).sendKeys("一小八中");
driver.findElement(By.id("searchbtn")).click();
driver.findElement(By.linkText("新疆")).click();
driver.findElement(By.linkText("二手房")).click();
vars.put("window_handles", driver.getWindowHandles());
PrintWriter out?= new?PrintWriter(outfile, "UTF-8");
for?(int?i?= 1; i?< 10; i++)
{
WebElement opentab=driver.findElement(By.cssSelector(".property:nth-child("+ i?+") .property-content-title-name:nth-child(1)"));
String elementTest=opentab.getText();
out.println(elementTest);
opentab=driver.findElement(By.cssSelector(".property:nth-child("+ i?+") .property-price-total-num"));
elementTest=opentab.getText();
out.print(elementTest);
out.println("W");
opentab=driver.findElement(By.cssSelector(".property:nth-child("+ i?+") > .property-ex .property-content-info-text:nth-child(2)"));
elementTest=opentab.getText();
out.println(elementTest);
out.println(" ");
?
}
out.close();
driver.close();
}
?
}
?
?
3.?實驗結果
?
圖2 實驗結果圖
?