1. Environment installation
Nodejs installation
Webdriver installation. You can install the driver of each browser to operate the browser.
Chrome Driver Installation
Each version of the browser uses webDriver differently. You can view the current version of Chrome by going to the top right corner of chrome -> Help -> About Chrome.
Jump gate
And add the driver directory to the environment variable
Other browser drivers can be found in the following list:
- Firefox: github.com/mozilla/gec…
- Edge:developer.microsoft.com/en-us/micrs…
- Safari:webkit.org/blog/6900/w…
2, code to achieve automated testing
npm init
npm install selenium-webdriver
// index.js
const {Builder, By, Key, until} = require('selenium-webdriver');
const {Options} = require('selenium-webdriver/chrome');
var driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(new Options().excludeSwitches(["ignore-certificate-errors".'user-data-dir="C:\\Users\\qiang\\AppData\\Local\\Google\\Chrome\\User Data"']))
.build();
driver.get('https://image.baidu.com/')
.then(function () {
driver.findElement(By.id('search')).then(async (res)=>{
console.log('res',await res.getText())
})
}).catch(function (e) {
console.log(e);
});
Copy the code
3, By XPATH and CSS positioning comparison
4. Reference article:
Selenium WebDriver
Selenium- node. js: webdriver usage
Selenium- WebDriver basic use
The Selenium-WebDriver framework commonly uses basic operations
Selenium uses CSS to locate page elements