Selenium basics 1

Selenium is a tool for web application testing. Selenium tests run directly in the browser, just like a real user is operating. Supported browsers include IE (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, etc.

Commonly used of course is to simulate Firefox and Chrome, after all, it is convenient for debugging.

Through this way of visualization and imitating human browsing, it is easier and more intuitive to obtain information on the page. For the rookie, it is still relatively friendly, and it is good to use F12 to view the elements.

manual:

1. Download the corresponding browser webdriver

2. Write python script

Download the webdriver corresponding to the browser version:

Firefox: https://github.com/mozilla/geckodriver/releases/

Chrome: http://chromedriver.storage.googleapis.com/index.html

 

Scripting

First import the webdriver module of the selenium library

from selenium import webdriver

According to needs, sometimes the page loads slowly. To wait for the page to finish loading, you need to import the waiting module again

from selenium.webdriver.support.ui import WebDriverWait

First, start webdriver, here is Chrome as an example (note the case, the excutable_path parameter is used to specify the path of the downloaded chromedriver.exe)

browser = webdriver.Chrome(executable_path=r'D:\用xxx\chromedriver.exe')

After the operation is completed, you will find that a chrome browser is opened, and then you need to let the browser load the web page url with the get method

browser.get(url)

At this time, the browser webdriver opens a page, and the next step is to locate the page. The effect of the page here is not the effect of right-click -> source code, but the effect of F12, and you can start element positioning. There are a variety of webdriver methods for element positioning, you can refer to this article:

The most complete in history! 30 ways to locate Selenium elements

https://blog.csdn.net/qq_32897143/article/details/80383502

List several methods for positioning, Xpath is relatively powerful. Everyone orders food on demand.

# 获取id标签值
element = browser.find_element_by_id("passwd-id")
# 获取name标签值
element = browser.find_element_by_name("user-name")
# 获取标签名值
element = browser.find_elements_by_tag_name("input")
# 也可以通过XPath来匹配
element = browser.find_element_by_xpath("//input[@id='passwd-id']")

In addition to selection and crawling, there are also functions to imitate clicking and input. For details, please refer to this article:

Selenium tutorial

https://www.jianshu.com/p/6c82c965c014

As far as I know, in addition to python, java can also be used, and the function is still very powerful.

Guess you like

Origin blog.csdn.net/u010472858/article/details/103271439