import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.annotations.Test; public class Headless { @Test public void headless() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe"); String url = "http://www.qixin.com/"; //启信宝首页 String companyUrl; //要获取的公司信息地址 String companyName; //要获取的公司名称 String code_1; //统一社会信用代码 String code_2; //注册号 ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("headless"); WebDriver driver = new ChromeDriver(chromeOptions); //使用无头浏览器 driver.get(url); Thread.sleep(2000); driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[1]/div/div/div/div[2]/div[8]/a")) .click(); //点击请登录 Thread.sleep(3000); driver.findElement(By.xpath("/html/body/div[2]/div/div/div[2]/div/div/div/div/div[1]/input")) .sendKeys("13688888888"); driver.findElement(By.xpath("/html/body/div[2]/div/div/div[2]/div/div/div/div/div[2]/input")) .sendKeys("******"); driver.findElement(By.xpath("/html/body/div[2]/div/div/div[2]/div/div/div/div/div[4]/a")) .click(); //登录 Thread.sleep(3000); companyUrl = driver.findElement(By.xpath("/html/body/div[1]/div[4]/div/div[2]/div/div[1]/div[1]/a")) .getAttribute("href"); driver.navigate().to(companyUrl); Thread.sleep(3000); companyName = driver.findElement(By.xpath("/html/body/div[6]/div/div[2]/div/div/h4")) .getText(); code_1 = driver.findElement(By.xpath("//*[@id=\"icinfo\"]/table/tbody/tr[1]/td[2]")) .getText(); code_2 = driver.findElement(By.xpath("//*[@id=\"icinfo\"]/table/tbody/tr[2]/td[2]")) .getText(); System.out.println("公司名称:"+companyName+"\n"+ "统一社会信用代码:"+code_1+"\n"+ "注册号:"+code_2); driver.quit(); } }
Selenium ChromeDriver headless爬启信宝
最新推荐文章于 2023-11-09 23:30:00 发布