java代码
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class crawler { public static void main(String[] args) throws InterruptedException { //设置webdriver驱动的位置 System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Mozilla Firefox\\geckodriver.exe"); WebDriver webDriver = new FirefoxDriver(); webDriver.manage().window().maximize(); webDriver.navigate().to("https://weibo.com/"); Thread.sleep(10000); //定位到用户名输入框 WebElement username = webDriver.findElement(By.xpath("//*[@id=\"loginname\"]")); //定位到密码输入框 WebElement password = webDriver.findElement(By.xpath("//*[@id=\"pl_login_form\"]/div/div[3]/div[2]/div/input")); //输入用户名 username.sendKeys("*****"); //输入密码 password.sendKeys("*****"); Thread.sleep(1000); WebElement login = webDriver.findElement(By.xpath("/html/body/div[1]/div[1]/div/div[2]/div[1]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div/div/div/div[3]/div[6]/a")); login.click(); Thread.sleep(5000); //登陆后等待5s让js等异步内容加载完成,最后输出页面html System.out.println(webDriver.getPageSource()); } }
pom.xml里的依赖
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.12.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-firefox-driver</artifactId> <version>3.12.0</version> </dependency> </dependencies>
firefox webdriver下载地址https://github.com/mozilla/geckodriver/releases/。
运行效果,浏览器中登陆成功,idea里面可以输出页面html。
原文地址:https://www.cnblogs.com/littlechicken/p/9162893.html
时间: 2024-10-13 06:32:59