Java code
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 {//Set Webdriver driver's position 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); Navigate to the User name input box webelement username = webdriver.findelement (By.xpath ("//*[@id =\" loginname\ "]"); Navigate to the Password entry box webelement password = webdriver.findelement (By.xpath ("//*[@id =\" pl_login_form\ "]/div/div[3]/div[2]/div/ Input ")); Enter the user name Username.sendkeys ("* * * * *"); Enter Password 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); After landing wait for 5s to let JS and other asynchronous content loading complete, the final output page HTML System.out.println (Webdriver.getpagesource ()); }}
Dependence in the 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 webdriverhttps://github.com/mozilla/geckodriver/releases/.
Operation effect, the browser login success, idea inside can output page HTML.
Java+selenium simulation landing Sina Weibo demo