1、使用google浏览器,下载插件
2、下载Selenium Standalone Server包
3、录制
5、添加包
6、javacsv.jar
开始的时候导入的是opencsv.jar
7、修改代码进行批量处理
遇到的问题
使用chromedriver 要下载和chrome版本对应的chromedriver并将chromedriver.exe放到系统变量path可以找到的地方
package gtBailly; import java.io.IOException; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collection; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.Select; import com.csvreader.CsvReader; @RunWith(Parameterized.class) public class Selenium { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); private String username,password,git; public Selenium(String username,String git){ this.username=username; this.password=username.substring(4); this.git=git; } @Before public void setUp() throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Parameterized.Parameters public static Collection<Object[]> getData() throws IOException { Object[][] obj = new Object[97][]; CsvReader reader = new CsvReader("C:\\Users\\GT\\Desktop\\input.csv", ',', Charset.forName("GBK")); int num = 0; reader.readHeaders(); while(reader.readRecord()){ obj[num] = new Object[]{reader.get(0), reader.get(1)}; num++; } return Arrays.asList(obj); } @Test public void testSelenium() throws Exception { driver.get("https://psych.liebes.top/st/"); driver.findElement(By.id("username")).clear(); driver.findElement(By.id("username")).sendKeys(this.username); driver.findElement(By.id("password")).clear(); driver.findElement(By.id("password")).sendKeys(this.password); driver.findElement(By.id("submitButton")).click(); assertEquals(this.git, driver.findElement(By.xpath("/html/body/div/div[2]/a/p")).getText()); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } } }