当前位置:网站首页>Getting started with webdriver

Getting started with webdriver

2022-06-12 14:45:00 Micro blog

webdriver As selenium New version of plug-ins , and selenium RC The biggest difference is that it does not need to start the service , Direct operation Java The code can be .

Create a new one maven project ,pom The contents of the document are as follows , So you can selenium Official download of webdriver The package needed

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>Selenium2Test</groupId>

<artifactId>Selenium2Test</artifactId>

<version>1.0</version>

<dependencies>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>2.44.0</version>

</dependency>

<dependency>

<groupId>com.opera</groupId>

<artifactId>operadriver</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>com.opera</groupId>

<artifactId>operadriver</artifactId>

<version>0.16</version>

<exclusions>

<exclusion>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-remote-driver</artifactId>

</exclusion>

</exclusions>

</dependency>

</dependencies>

</dependencyManagement>

</project>

 

 

selenium webdriver call Chrome Methods   

 

First download a chromedriver The driver

The following websites can be downloaded to various versions Chrome Corresponding webdriver

http://chromedriver.storage.googleapis.com/

http://npm.taobao.org/mirrors/chromedriver/

selenium webdriver The establishment of the project has already been mentioned , For any browser , It's all consistent

Need assurance chromedriver and Chrome Version correspondence , If the two versions do not correspond , It may not be able to be adjusted normally Chrome

The implementation is as follows

 

"//Chrome The path of the execution file

    System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

//Chrome Of driver The path of , Relative path to the root path of the current project

    ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File("driver\\chromedriver2.37.exe"))

.usingAnyFreePort().build();

service.start();

// initialization driver

    WebDriver driver = new RemoteWebDriver(service.getUrl(),DesiredCapabilities.chrome());

// Open the browser

driver.get(");

 

 

 

 

webdriver call Firefoxpackage lesson1;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

 

publicclassExampleForFireFox{

publicstaticvoid main(String[] args){

// If your FireFox Not installed in the default directory , Then it must be set in the program

System.setProperty("webdriver.firefox.bin",

"D:\\Program Files\\Mozilla Firefox\\firefox.exe");

// Create a FireFox Browser instance of

WebDriver driver =newFirefoxDriver();

// Let the browser access Baidu

driver.get("http://www.baidu.com");

// The following code can also be used to realize

// driver.navigate().to("http://www.baidu.com");

// obtain Web page title

System.out.println("1 Page title is: "+ driver.getTitle());

// adopt id find input Of DOM

WebElement element = driver.findElement(By.id("kw"));

// Enter key

element.sendKeys("zTree");

// Display the... Of the search results page title

System.out.println("2 Page title is: "+ driver.getTitle());

// Close the browser

driver.quit();

}

}

webdriver Same as selenium RC equally , Can combine selenium IDE To use , stay Firefox Install in selenium IDE plug-in unit

stay Firefox Select add ons , Search for selenium  IDE, Select the following plug-ins to install

After installation , In the component list and Firefox See... In the toolbar of selenium IDE The icon of

 

 

Use IDE Record browser operations , And will operate export by Junit4 Code for , You can import selenium webdriver The project used

 

There are several problems when using the code exported above

First ,selenium webdriver Not in China Junit Of jar package , This is easier to solve , Import directly Junit Of jar that will do , What I introduce here is Junit 4.11 Version of jar

Then there was another problem , This class introduces a package ,import static org.hamcrest.CoreMatchers.*;  But this package is not used in this class , I wanted to import Remove it , After the result is removed, an error is reported , Say less about this bag , Then ask for Helped Baidu , be supposed to Junit The new version of the jar It doesn't contain hamcrest My bag , But actually this jar stay Junit Testing is a must , Then we can only introduce this package again , Sure enough , It is introduced. hamcrest-core-1.3.jar After this bag , The code won't say wrong

Re execution , Hint not found Firefox Installation file , I read some reference tutorials and said Firefox If installed in the default directory (D:\Program Files\Mozilla Firefox) No configuration is required , However, although this is the default installation directory , The result is still not found , Then you can only add the configuration specification Firefox The directory.

System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");

After the above adjustment , The code has been able to execute normally and tune up Firefox 了

 

come from <http://lxw198902165221.blog.163.com/blog/static/25895002220171233850323/>

 

 

原网站

版权声明
本文为[Micro blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121418275742.html