The first query script for Python-based Web automation (Selenium)

Source: Internet
Author: User

Now that the Web Automation environment has been set up, it's time to start writing scripts. Let's start with a relatively simple script, as follows:

#coding = Utf-8

From selenium import Webdriver

Import time

Try

Driver = Webdriver. Chrome ()

# driver = Webdriver. Firefox ()

Driver.maximize_window ()

# driver.set_window_size (1200, 900)

Driver.get (' https://www.baidu.com/')

driver.find_element_by_id ("kw"). Send_keys ("Selenium2")

driver.find_element_by_id ("su"). Click ()

# driver.find_element_by_id ("su"). Submit ()

Time.sleep (5)

Except Exception as E:

Print (e)

Finally

# Driver.quit ()

Driver.close ()

Practice Explanation: First, PythonKnowledge 1, " #coding = utf-8 "

PY file is not supported in Chinese, even if you input the comment is not Chinese, in order to solve this problem, you need to change the file encoding type to UTF-8 type, enter this code can let the py source file in Chinese.

It is recommended that you write the code before you add this sentence, because whether it is a comment or pop-up message prompts, unavoidable to input Chinese, so this basic is necessary.

2, " from...import ...and Import"2.1 From...import: Only one class in a package is introduced

Example: fromSelenium import Webdriver

Import the Webdriver class in the Selenium package

2.2 Import: Is the introduction of the entire datetimePackage

For example:import time

Import Time Entire Package

3, " Try:     ..... except Exception as E: ..... finally:....."

Python runs out of the exception usage method, and the code continues to run the following code after an error occurs.

Second, the operation failed

It may not be successful to run the above code directly, and if it fails, you need to check if the browser driver is installed:

Chrome:chromedriver

Firefox:geckodriver

Ie:iedriverserver

These drivers are placed directly in the Python installation directory, as long as the configuration environment variable is added, it can be run.

Three, simple to understand 1, Driver = Webdriver. Chrome ()

Assign the Webdriver chrome object to the variable driver. Only after you have obtained the browser object, you can launch the browser, open the URL, manipulate the page elements.

2. Modify the size of the browser window (two ways) 2.1 Driver.maximize_window ():

Directly modified to maximize

2.2 driver.set_window_size (.)

Specify Dimension Size

3. driver.get (' https://www.baidu.com/')

After getting the browser object, you can send the URL to the browser via the Get () method

4. driver.find_element_by_id ("kw"). Send_keys ("Selenium2") and driver.find_element_by_id ("su"). Click ()

URL to the page, according to the automation requirements of the page elements to get, and to do the appropriate action.

4.1 driver.find_element_by_id ("kw"). Send_keys ("Selenium2")

Here through the id=kw, to locate the input box Baidu, and through the keyboard Input Method Send_keys () to enter the Baidu input box "Selenium2" keyword search.

4.2 driver.find_element_by_id ("su"). Click () and driver.find_element_by_id ("Su"). Submit ()

This step through the ID=SU positioning "Baidu" search button, and to the search button to send a click of the event clicking () (or submit ()).

5. Time.sleep (5)

Wait 5 seconds.

6. driver.quit () and driver.close ()

Exit and close the browser and related drivers, only one page, two effects are the same, if you open more than one page is different.

Driver.quit (): Exits the drive and closes all associated windows

Driver.close (): Close the current window

Run the script

Run successfully

The first query script for Python-based Web automation (Selenium)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.