Selenium Webdriver Java Simple example

Source: Internet
Author: User

  1. Development environment
    JDK
    :
    Http://www.oracle.com/technetwork/java/javase/downloads/index.html

    Eclipse:
    : http://www.eclipse.org/downloads/

    Selenium Jar Package
    (Used here: Selenium-java-2.45.0.zip, Selenium-server-standalone-2.45.0.jar)
    : http://code.google.com/p/selenium/downloads/list

  2. Development tools  
    Open Eclipse, create a new Java project, create a new Lib folder under the project  
    Selenium-java-2.45.0.zip the extracted files and Selenium-server-standalone-2.45.0.jar, copy them all in Span class= "Apple-converted-space" > 

    PS: In fact, these jars, there is no matter, when the reference to an absolute path can be, There are selenium corresponding browser version also has strict requirements, for different Firefox browser, selenium version is also different, if only Java project start Webdriver, Then you must have the corresponding version of Selenium-server-standalone-2.45.0.jar

    Next build Path 
    Project directory Right –>build path–> config build path–>java  Build Path–>libraries–>add jars 
    to add all the jars under Lib, Including Selenium-server-standalone-2.45.0.jar, and then all the way Ok, 

    You will see a more mapped class library under the project referenced Libraries 

  3. code example
    Preparation to do here, the next thing to do is to start writing code, in the SRC folder, create a new package, create a new class

The code is as follows:
(Launch browser, Baidu search HelloWorld, after the end of the run, close the browser)

PackageCom. Selenium. Test; Import org. Openqa. Selenium. By; Import org. Openqa. Selenium. Webdriver; Import org. Openqa. Selenium. Webelement; Import org. Openqa. Selenium. Firefox. Firefoxdriver;p Ublic class Seleniumhello {public static void main (string[] args) {//If Firefox is not installed by default on the C drive, it needs to develop its path System. SetProperty ("Webdriver.firefox.bin","D:/program Files (x86)/mozilla Firefox/firefox.exe"); Defines the drive object as a Firefoxdriver object Webdriver driver = new Firefoxdriver (); Driver URL Driver. Get ("http://www.baidu.com/")//browser window becomes larger driver.manage () .window () .maximize () //Position input box element webelement Txtbox = Driver.findelement (By< Span class= "Hljs-preprocessor" >.name ( "WD")) ;//Enter text in the input box Txtbox.sendkeys ( "HelloWorld") //positioning button element webelement btn = Driver.findelement (By.id ( "su")) ;//click button Btn.click () .close () Span class= "hljs-comment" >; }} 
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • *
    • all
    • +
    • -
    • -
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

Selenium Webdriver Java Simple example

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.