当前位置:网站首页>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/>
边栏推荐
- 面向优化科学研究领域的软件包
- Appnium (II) installation and basic use of mitmproxy
- PMP敏捷知识点
- Nesting of C language annotations
- Yiwei lithium energy plans to raise 9billion yuan: liujincheng and luojinhong jointly subscribe for 6billion yuan of layout Optical Valley
- [wechat applet] 3 The first wechat applet
- Player actual combat 25 unpacking module add close
- Printing colored messages on the console with printf
- C secret script Chapter 1: data storage (in-depth analysis) supplement
- 【LDA】基础知识笔记——主要是AE、VAE
猜你喜欢

【OCR】AspriseOCR C# 英文、數字識別(中文不行)

selenium-webdriver之断言

Tu oses le croire? Il m'a fallu deux jours pour développer un système de gestion.

Player practice 20 audio thread and video thread

Copy word content to excel and automatically divide it into multiple columns

基于TensorRT的深度学习模型部署实战教程!

Analysis of two-dimensional array passing as function parameter (C language)

你敢信?開發一個管理系統我只用了兩天
![[wechat applet] 3 The first wechat applet](/img/40/1571c11363e72f5a1e932586a1f240.jpg)
[wechat applet] 3 The first wechat applet

我愿称之为史上最全的深度学习面经总结(附答案详解)
随机推荐
C secret script Chapter 3 (detailed explanation of string function) (Section 2)
Tu oses le croire? Il m'a fallu deux jours pour développer un système de gestion.
Sorting out the differences between ABS () and Fabs () in C language
Appnium (I) basic use of appnium
En langage C, la fonction principale appelle une autre fonction et assemble le Code pour comprendre
[gatewaysessioninactiveevent event] 1 Connect your own client disconnect
selenium进阶
QT realize picture dragging
Jetpack架构组件学习(3)——Activity Results API使用
[datetmeformatter] realize the conversion between localdatetime and text
sql跨库注入
工业机械臂(机器人)视觉定位引导系统
PMP agile knowledge points
Software package for optimization scientific research field
MySQL index and view
C语言中主函数调用另外一个函数,汇编代码理解
Energy chain smart electronics landed on NASDAQ: Bain is the shareholder to become the first share of charging services in China
G++ error in compiling Win32 program: undefined reference to '__ imp_ GetStockObject‘
ADB command (2) use monkey to test
[wechat applet] 4 Introduction to wechat developer tools