It is the first time to use Selenium to control Google browser and carry out some simple automation operations

Posted by Ambush Commander on Sun, 30 Jan 2022 07:47:06 +0100

Attach the official website link first selenium and Document link

The introduction and examples on the official website are relatively simple and easy to use. You can refer to the official documents for learning.


Selenium is a comprehensive project of a series of tools and libraries that support the automation of web browsers.

Selenium controls the web browser.

Selenium has many functions, but its core is a tool set for web browser automation. It uses the best technology to remotely control browser instances and simulate the interaction between users and browsers.

It allows users to simulate common activities performed by end users; Enter text into the field, select the drop-down value and check box, and click the link in the document. It also provides many other controls, such as mouse movement, arbitrary JavaScript execution, and so on.

Although Selenium is mainly used for front-end testing of websites, its core is browser user agent library. These interfaces are ubiquitous in applications, and they encourage combinations with other libraries to meet your purposes.

1. Create maven project in POM Adding dependencies to XML

 		//selenium dependency
		//Chrome driver dependency. Use that browser to add that browser
        //Firefox driver depends on. You can use that browser to add that browser

2. Download the driver and configure the path

First, check the browser version in the browser settings and download the corresponding driver, chrome image of Taobao.
After decompression, get chromedriver exe

Put chromedriver Add the current path where exe is located to the path or add chromedriver Exe to the existing path path or set it manually in the program.

3. First script

    public  void  chromeDriverTest() throws InterruptedException {
//If chromedriver If the current path of exe is added to the path, you do not need to set it here. If it is invalid, you can restart idea and try again.
//        System.setProperty("","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        //Open a session, that is, open the browser
        ChromeDriver chromeDriver = new ChromeDriver();
        //Program pause for 5 seconds
        //End the session and close the browser

The browser is opened and on the home page of Taobao.

4. Do some tests.

How to locate some elements? Post a picture of the official document.

Using the form $("id") in jquery is similar in selenium. Relevant methods can be called through the static class By. Get the required element.

How to locate the element you want to locate in the browser. F12 open the browser console, click the small button with mouse in the upper left corner of the console, and then click the element to be located. The console will automatically jump to the Elements page and select the element. Right click copy. There are many ways to choose. You can try it. You can see that if the input box id of Taobao home page is q, you can use by id () location.

Once you know how to locate the element, use webelement element = driver Findelement (by. ID ("Q")) gets and saves the current element. RE element Sendkeys ("tablet") enter the value, locate the search button again, and click it to automatically conduct a search. Paste a picture and successfully complete the search.
Note: Taobao needs to log in to search. You can set the web address to , you don't have to log in.

5. Some other examples on the official website

Gets the value in all li.

List<WebElement> elements = driver.findElements(By.tagName("li"));

for (WebElement element : elements) {
    System.out.println("Paragraph text:" + element.getText());

wait for

WebDriver usually has a blocking API. Because it is an out of process library that instructs the browser what to do, and the web platform is asynchronous in nature, WebDriver does not track the real-time activity state of DOM. This is accompanied by some challenges that we will discuss here.

According to experience, most of the intermittent problems caused by the use of Selenium and WebDriver are related to the competitive conditions between browser and user instructions. For example, the user instructs the browser to navigate to a page and then gets an error of no such element when trying to find an element. An example.

    public  void  firstWebDriverTest(){
        WebDriver driver = new ChromeDriver();
        driver.findElement("kw")).sendKeys("cheese" + Keys.ENTER);
// Initialize and wait till element(link) became clickable - timeout in 10 seconds
        WebElement firstResult = new WebDriverWait(driver, 10)
// Print the first result


10 is the maximum waiting time. After searching for cheese, if the network is poor or there is no corresponding result in the page for other reasons, the located element will not be found and the error of no such element will appear until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"2\"]/div/div/h3/a"))); It will test whether there is a located element in the waiting ten seconds. If it is found, it will continue to run the program. If not, it will wait for a while.

Topics: Selenium chrome