Java+selenium simulation landing Sina Weibo demo

Source: Internet
Author: User
Tags xpath seleniumhq

  

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.