当前位置:网站首页>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/>
边栏推荐
- MySQL index and view
- C secret arts script Chapter 5 (structure) (Section 2)
- Unhandled exception stack overflow
- Server concurrency - note 1
- Player actual combat 21 audio and video synchronization
- Two methods of QT using threads
- NetCore结合CAP事件总线实现分布式事务——入门(1)
- How to set, reset and reverse bit
- En langage C, la fonction principale appelle une autre fonction et assemble le Code pour comprendre
- Junit多线程的写法
猜你喜欢

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

我愿称之为史上最全的深度学习面经总结(附答案详解)

【LDA】基础知识笔记——主要是AE、VAE

Player practice 26 adding slider and window maximization

华为设备配置OSPF伪连接

C语言中主函数调用另外一个函数,汇编代码理解

【LDA】EM变分推理 粗略版笔记【待完善

【OCR】AspriseOCR C# 英文、数字识别(中文不行)

掌门教育被强制退市:上市仅一年时间 软银CMC损失惨重

JS (II) syntaxerror: cannot use import statement outside a module
随机推荐
【LocalDate LocalTime LocalDateTime】1. Using immutability to achieve thread safety 2 Current date, current time, current date time 3 Since the time zone is not considered, you need to add 8 hours to th
Detailed explanation of C language memset
[OCR] aspriseocr C # English, number recognition (not Chinese)
Vs2012: cannot assign a value of type 'char *' to an entity of type 'lpwstr'
Two months' experience in C language
QT realize picture dragging
【OCR】AspriseOCR C# 英文、數字識別(中文不行)
【Optional】1. Map and ifpresent 2 Ofnullable and orelse
模块八
recursive learning
三维重建系统 | L3双视角运动恢复结构(SFM双目SFM)
C secret arts script Chapter 5 (structure) (Section 2)
能链智电登陆纳斯达克:贝恩是股东 成中国充电服务第一股
Module VIII
【Calendar】1. For globalization 2 But only date can be formatted by SDF 3 Month to -1 4 Week from Sunday is the first day
selenium-webdriver之断言
三维重建系统 | L3增量运动恢复结构(增量SFM)
C secret arts script Chapter 2 (detailed explanation of pointer) (Section 1)
C secret script Chapter 3 (detailed explanation of string function) (Section 1)
Use Baidu AIP to obtain the text in the specified area of the screen