Web automation framework: selenium learning and operation encyclopedia (Python version)

1. Browser driver download

Selenium requires a browser driver to interact with the browser of your choice. For example, Firefox requires geckodriver to be installed. Make sure it's in PATH.

The download addresses for mainstream browser drivers are as follows:

Browser Driver name How to open and precautions address
Chrome chromedriver driver = webdriver.Chrome()
Download the version of chromedriver.exe corresponding to the browser
Be sure to create the object, otherwise it will crash when you open it! ! !
https://registry.npmmirror.com/binary.html?path=chromedriver
Firefox geckodriver webdriver.Firefox()
Download the latest version of geckodriver.exe
https://github.com/mozilla/geckodriver/releases
Edge MicrosoftWebDriver driver = webdriver.Edge()
It is best to download the corresponding version of the browser from the official website:https://www.microsoft.com/en-us/edge, Then download the browser's corresponding version of msedgedriver.exe
Be sure to create the object, otherwise it will crash when you open it! ! !
When running the code in Windows 10: Rename msedgedriver.exe to MicrosoftWebDriver.exe, otherwise an error will be reported when running the program! ! !
When running code in Windows 11: Do not rename msedgedriver.exe! ! !
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver
IE IEDriverServer webdriver.Ie()
Download the selenium corresponding version of IEDriverServer.exe
https://registry.npmmirror.com/binary.html?path=selenium

For details, you can view:The disputes between chromedriver, geckodriver, Microsoft Web Driver, IE Driver Server and opera driver
ps: Download address of the latest version of chromedriver :
https://googlechromelabs.github.io/chrome-for-testing/#stable

2. Selenium-python installation (open website, operate elements)

1.Installation

pip install -U selenium

Note:-U specifies to download the latest version of selenium
2. Control the browser
2.1 Open the browser

from selenium import webdriver
# 打开浏览器
driver = webdriver.Chrome()

Alternatively, you can specify the driver path:

driver = webdriver.Chrome(executable_path=r'D:\Program Files\Python36\chromedriver.exe')

Close the browser and its corresponding driver:

driver.quit()

You can also use context to control its automatic shutdown after performing operations:

with webdriver.Chrome(executable_path='chromedriver.exe') as driver:
    driver......

2.2 Open the website

driver.get('https://blog.csdn.net/testleaf/article/details/123269042')

Delay 3 seconds:

import time
time.sleep(3)

2.3 Positioning elements
The legendary eight element positioning methods:

driver.find_element_by_id()                 # 通过id属性定位(唯一);常用
driver.find_element_by_xpath()              # 通过xpath表达式定位;常用
driver.find_element_by_class_name()         # 通过类名定位;常用
driver.find_element_by_name()               # 通过name属性定位
driver.find_element_by_tag_name()           # 通过标签名定位
driver.find_element_by_css_selector()       # 通过css选择器定位
driver.find_element_by_link_text()          # 通过链接标签的text类容定位
driver.find_element_by_partial_link_text()  # 通过匹配链接标签的text类容定位

The above methods all find the first element. Each method has methods corresponding to multiple elements:
For example:driver.find_elements_by_id()

示例1:
driver.find_element_by_id(“toolbar-search-input”)
Insert image description here
示例2:
driver.find_element_by_name(“wd”)
Insert image description here
2.4 操作元素

# 定位搜索框
element = driver.find_element_by_id("toolbar-search-input")
# 输入搜索内容
element.send_keys('web自动化框架:selenium入门')
# 定位搜索按钮
search_button = driver.find_element_by_id('toolbar-search-button')
# 点击搜索按钮
search_button.click()

3. Web page analysis (HTML, xpath)

1.HTML
HTML [Hyper Text Markup Language]: Hypertext markup language, not a programming language, used to describe web pages. Also known as: web page source code, html source code, html document, document.
Specifically, you can view:
Web page source code, html source code, html document, document
HTML common tags
HTML example:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #table {
      
      
            border: 1px solid
        }

        .th {
      
      
            font-size: 20px
        }
    </style>
</head>
<body>
<div>我是一个div标签</div>
<h1>我是一个大<span style="color:red">标签</span></h1>
<p></p>
<a href="https://blog.csdn.net/testleaf">testleaf</a>
<table id="table">
    <thead>
    <tr>
        <th class="th aaa">字段1</th>
        <th class="th">字段2</th>
        <th class="th">字段3</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    </tbody>
</table>
<form action="">
    <p>用户名: <input type="text"></p>
    <p>密码:<input type="password"></p>
    <p><input type="submit"></p>
</form>
</body>
</html>

2.xpath
xpath: a language for parsing html/xml.

  • grammar
    • Select node
      • nodename selects all child nodes of this node
      • / Select from the root node
      • // Select nodes in the document from the current node matching the selection, regardless of their position
      • . Select the current node
      • … means selecting the parent node of the current node, such as://div[@class="active"]/..
      • @Select attribute
      • Case
        • //divGet all divs in the document
    • Predicate is used to find a specific node, or a node containing a specified value
      • The predicate is enclosed in square brackets
      • //div[@class="active"]
      • //div[@class="active" and @id="kw"]
      • //div[@class="active" or @id="kw"]
      • //div[@class="active"]/span[last()]
    • Axis is used to find nodes relative to the current node
      • Usage syntax axis-name::node-name[predicate]
      • ancestor selects all parent nodes of the current node
      • parent selects the parent node of the current node
      • like://div[@class="active"]/child::book
      • …详见 https://www.w3school.com.cn/xpath/xpath_axes.asp
    • function
      • text() text content of the element
        • //li[text()="强烈推荐"]
      • The content contained in contains(@attribute name/text(), value)
        • //li[contains(@class, "ls")]

Verify in browser:

$x('//div')

Insert image description here

4. Basic operations of selenium

Pycharm-Terminal operation:

IPYTHON
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
driver.quit()
exit()

Insert image description here
Browser-Console operation:

window.scrollTo(0,100)

Insert image description here

1. Eight methods of element positioning

The legendary eight element positioning methods:

driver.find_element_by_id()                 # 通过id属性定位(唯一);常用
driver.find_element_by_xpath()              # 通过xpath表达式定位;常用
driver.find_element_by_class_name()         # 通过类名定位;常用
driver.find_element_by_name()               # 通过name属性定位
driver.find_element_by_tag_name()           # 通过标签名定位
driver.find_element_by_css_selector()       # 通过css选择器定位
driver.find_element_by_link_text()          # 通过链接标签的text类容定位
driver.find_element_by_partial_link_text()  # 通过匹配链接标签的text类容定位

The above method will only return the first matched element;
returns the object of selenium.webdriver.remote.webelement.WebElement;
To return multiple elements, just add s after element, for example: driver.find_elements_by_xpath(), be careful when using it;
anything elements The method returns a list;

from selenium import webdriver

driver = webdriver.Chrome()

# 打开百度页面
driver.get('https://www.baidu.com')

# 1. 获取搜索框
search_input = driver.find_element_by_id('kw')
print(type(search_input))
print('1.搜索框的name属性值=', search_input.get_attribute('name'))

# 2. 搜索按钮
search_btn = driver.find_element_by_xpath('//input[@value="百度一下"]')
print('2.搜索按钮的id=', search_btn.get_attribute('id'))

# 3. 百度logo
logo = driver.find_element_by_class_name('index-logo-src')
print('3.百度logo的src=', logo.get_attribute('src'))

# 4. 通过name属性定位搜索框
search_input_by_name = driver.find_element_by_name('wd')
print('4.搜索框的id=', search_input_by_name.get_attribute('id'))

# 5. 热搜榜中的链接
hot_ul = driver.find_element_by_xpath('//ul[@id="hotsearch-content-wrapper"]')
# hot_ul = driver.find_element_by_xpath('//ul[@text()="热榜"]')
hot_a_s = hot_ul.find_element_by_tag_name('a')
print('5.热搜榜第一的标题是:', hot_a_s.text)

# 6. 通过css选择器定位搜索框
search_input_by_css = driver.find_element_by_css_selector('#kw')
print('6.搜索框的name=', search_input_by_css.get_attribute('name'))

# 7. 新闻栏目
el_a = driver.find_element_by_link_text('新闻')
print('7.新闻栏目的url=', el_a.get_attribute('href'))

# 8. 通过部分text匹配新闻栏目
el_a1 = driver.find_element_by_partial_link_text('新')
print('8.新闻栏目的url=', el_a1.get_attribute('href'))

driver.quit()

2. Dynamic positioning of elements

Problem: The positioning method of the element is uncertain. It may be id or xpath. The positioning method needs to be dynamically specified in different ways;
Solution: Add a judgment statement, which does not require us Write, this is how Selenium’s underlying positioning is done;

driver.find_element(by,value)
driver.find_elements(by,value)

by: positioning method
value: positioning expression

import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

# 打开百度页面
driver.get('https://www.baidu.com')

id_loc = 'kw'
xpath_loc = '//input[@id="kw"]'

e1 = driver.find_element(By.ID, id_loc)
# send_keys往输入框输入文本
e1.send_keys('我通过id定位')
time.sleep(3)

# e2 = driver.find_element(By.XPATH, xpath_loc)
e2 = driver.find_element('xpath', xpath_loc)
e2.clear()  # 清空原有内容
e2.send_keys('我通过xpath定位')

time.sleep(3)
driver.quit()

3. iframe switching

When the page contains an iframe, you cannot directly operate the elements in the iframe. You need to switch to the iframe first;
selenium switches through driver.switch_to.frame(frame_reference) ;
Selenium has three ways to switch iframe:

  • 1. Via webelement:
    • Using webelement to switch is the most flexible option, first locate the iframe and then switch to it;
  • 2. By name or id:
    • iframes generally have an id or name attribute, and you can use this attribute to switch. If the name or id is not unique on the page, the name will switch to the first one found;
  • 3. Switch through the index of the iframe in the page (starting from 0):
    • Use in the browser console debugging toolwindow.ampInaboxIframes to query the iframe in the current page; exit the iframe:driver.switch_to.default_content();
import time
from selenium import webdriver

# 使用with语句进行上下文管理
# 异常时也会关闭浏览器驱动
with webdriver.Chrome() as driver:
    driver.get('https://www.w3school.com.cn/tiy/t.asp?f=eg_html_form_radio')
    # 切换iframe
    # 1. webelement的方式
    # 先获取到iframe
    iframe = driver.find_element_by_id('iframeResult')
    # 再切换到
    driver.switch_to.frame(iframe)

    # 2. name/id的方式
    # 直接通过name/id切换
    driver.switch_to.frame('iframeResult')

    # 3. 使用索引
    # 切换到第二个iframe
    time.sleep(1)
    driver.switch_to.frame(1)
    # 找到female单选框
    female = driver.find_element_by_xpath('//input[@value="female"]')

    print(female)
    # 4. 离开iframe,回到主页面
    driver.switch_to.default_content()
    driver.find_element_by_xpath('//a[text()="运行代码"]')

4. Fill the form_fill the text box

import time
from selenium import webdriver

with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com')

    # 搜索框定位
    search_input = driver.find_element_by_id('kw')
    # element.send_keys(string)输入文本
    # 所有可输入的标签都适用
    search_input.send_keys('软件测试')

    # 搜索按钮定位
    search_btn = driver.find_element_by_xpath('//input[@value="百度一下"]')
    # 点击
    # element.click()点击
    search_btn.click()
    time.sleep(3)

5. Fill in the form_radio button

import time
from selenium import webdriver
with webdriver.Chrome() as driver:
    driver.get('https://www.w3school.com.cn/tiy/t.asp?f=eg_html_form_radio')
    # 切换iframe
    iframe = driver.find_element_by_id('iframeResult')
    driver.switch_to.frame(iframe)
    # 找到female单选框
    female = driver.find_element_by_xpath('//input[@value="female"]')
    # 在元素上点击
    female.click()  # 选中
    time.sleep(5)

6. Fill in the form_drop-down list

There are two ways to handle the drop-down box:
(1) Directly locate the option to be selected, and then click
(2) Through selenium .webdriver.support.ui.Select class

import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select

with webdriver.Chrome() as driver:
    driver.get('https://www.w3school.com.cn/tiy/t.asp?f=eg_html_elements_select')
    # 切换iframe
    iframe = driver.find_element_by_id('iframeResult')
    driver.switch_to.frame(iframe)
    # 找到audi选项
    # 1. 通过option直接操作
    option = driver.find_element_by_xpath('//option[@value="audi"]')
    option.click()
    time.sleep(1)
    # 2. 通过select类
    # 找到select
    select = Select(driver.find_element_by_xpath('//select[@name="cars"]'))
    #
    # # 选中索引为1的选项,索引从0开始
    select.select_by_index(1)
    time.sleep(1)
    #
    # # 选中value等于audi的选项
    select.select_by_value('audi')
    time.sleep(1)
    #
    # # 选中option的文本为Volvo的选项
    select.select_by_visible_text('Volvo')
    time.sleep(3)

7. Switch windows and tabs

webdriver does not distinguish between windows and tabs. Open a new tab or window and Selenium will use the window handle to handle it.
Each window has a unique identifier that is persistent across a single session.
(1) Get the current window handle: driver.current_window_handle;
(2) Switch windows or tabs: through loop traversal to switch;

import time
from selenium import webdriver
with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com')
    # 找到搜索框
    search_input = driver.find_element_by_id('kw')
    search_input.send_keys('图片')
    # 点击搜索按钮
    driver.find_element_by_id('su').click()
    time.sleep(1)
    # 获取原窗口的handle
    original_window = driver.current_window_handle
    print('当前窗口句柄', original_window)
    print('窗口的title', driver.title)

    # 选取第一个结果并点击
    driver.find_element_by_xpath('//div[@id="3001"]//a').click()
    # time.sleep(1)
    for handle in driver.window_handles:
        if handle != original_window:
            # 切换到新窗口
            driver.switch_to.window(handle)
            break
    # 打印当前窗口句柄
    print('新打开的搜索页面句柄', driver.current_window_handle)
    print('新打开的页面的title', driver.title)
    time.sleep(5)

8. Explicit wait

It takes time for the browser to render the page. If you position the element before rendering is completed, the element will not be found;
Therefore, you need to add a delay to wait. There are three reasons: A waiting method:
(1) time.sleep()
has been used repeatedly before; the waiting time is fixed and unstable; the waiting time is mostly uncertain;

(2) Explicit waiting
Explicit waiting is to loop before operating on an element to determine whether the conditions of the operation are met, and then operate after it is met;
selenium implements explicit waiting through objects of the selenium.webdriver.support.ui.WebDriverWait class; the webDriverWait class can receive 3 parameters when instantiated:

  • driver: webdriver object
  • timeout: timeout time, how many seconds to wait at most
  • poll_frequency: Check frequency, default 0.5 seconds

The until method accepts 1 parameter:

  • conditions: The condition is in the selenium.webdriver.support.expected_conditions module;
    Common conditions:
    presence_of_element_located: The element exists in the dom< /span> element_to_be_selected: The element is selectable element_to_be_clickable: The element is clickable
    visibility_of_element_located: The element is visible

The instantiation condition requires passing in a positioning parameter, which is a tuple:(by, loc_expression)

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import selenium.webdriver.support.expected_conditions as EC


with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com')
    # 找到搜索框
    search_input = driver.find_element_by_id('kw')
    search_input.send_keys('图片')
    # 点击搜索按钮
    driver.find_element_by_id('su').click()
    # 获取原窗口的id
    original_window = driver.current_window_handle
    print('当前窗口句柄', original_window)
    print('窗口的title', driver.title)

    # 选取第一个结果并点击
    #
    # WebDriverWait(driver, timeout=3).until(
    #     EC.visibility_of_element_located((By.XPATH, '//div[@id="3001"]//a'))
    # ).click()
    s_time = time.time()
    btn = WebDriverWait(driver, timeout=3).until(
        EC.visibility_of_element_located((By.ID, '3001'))
    )
    btn.click()
    e_time = time.time()
    print(e_time-s_time)
    # WebDriverWait(driver, timeout=3).until(
    #     EC.visibility_of_element_located(('id', '3001'))
    # ).click()
    # 会等待id为3001的元素可见,timeout=3表示最多等待3秒钟,超时就抛出异常
    # driver.find_element_by_xpath('//div[@id="3001"]//a').click()
    # time.sleep(1)
    for handle in driver.window_handles:
        if handle != original_window:
            # 切换到新窗口
            driver.switch_to.window(handle)
            break
    # 打印当前窗口句柄
    print('新打开的搜索页面句柄', driver.current_window_handle)
    print('新打开的页面的title', driver.title)
    time.sleep(5)

9. Implicit waiting

(3) Implicit waiting
Implicit waiting essentially sets a global waiting time. WebDriver will poll for a certain period of time when trying to find any element. By default, implicit waiting is Waiting is disabled.
Warning: Do not mix implicit waits and explicit waits, as this can result in unpredictable wait times.
For example: implicit waiting is set to 10 seconds, and explicit waiting is set to 15 seconds, which may cause a timeout to occur after 20 seconds;
Implicit waiting tells WebDriver if Poll the DOM for a period of time while looking for one or more elements that are not immediately available. The default setting is 0, which means disabled. Once set, the implicit wait is set for the lifetime of the session.

import time
from selenium import webdriver

with webdriver.Chrome() as driver:
    # 设置隐式等待5秒
    driver.implicitly_wait(5)
    driver.get('https://www.baidu.com')
    # 找到搜索框
    search_input = driver.find_element_by_id('kw')
    search_input.send_keys('图片')
    # 点击搜索按钮
    driver.find_element_by_id('su').click()
    # 获取原窗口的id
    original_window = driver.current_window_handle
    print('当前窗口句柄', original_window)
    print('窗口的title', driver.title)

    # 选取第一个结果并点击
    driver.find_element_by_xpath('//div[@id="3001"]//a').click()

    for handle in driver.window_handles:
        if handle != original_window:
            # 切换到新窗口
            driver.switch_to.window(handle)
            break
    # 打印当前窗口句柄
    print('新打开的搜索页面句柄', driver.current_window_handle)
    print('新打开的页面的title', driver.title)
    time.sleep(5)

10. Warning box

WebDriver provides an API for processing three types of native pop-up messages provided by JavaScript
(1) Alerts warning box

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Chrome() as driver:
    driver.get('https://www.w3school.com.cn/tiy/t.asp?f=js_alert')
    driver.switch_to.frame('iframeResult')

    button = WebDriverWait(driver, timeout=3).until(
        EC.visibility_of_element_located(('xpath', '//button')))
    # 有时候需要结合sleep来处理
    # time.sleep(1)
    button.click()
    # time.sleep(3)
    # 等待alert弹出

    alert = WebDriverWait(driver, timeout=3).until(EC.alert_is_present())
    # time.sleep(1)
    # alert = driver.switch_to.alert
    # 获取弹出框文本
    text = alert.text
    print(text)
    time.sleep(1)
    # # 确认
    alert.accept()
    time.sleep(1)

11. confirm confirmation box

(2) confirm confirmation box
Unlike the warning box, the confirmation box also has a cancel button;

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


with webdriver.Chrome() as driver:
    driver.get('https://www.w3school.com.cn/tiy/t.asp?f=js_confirm')
    driver.switch_to.frame('iframeResult')

    button = WebDriverWait(driver, timeout=3).until(
        EC.visibility_of_element_located(('xpath', '//button')))

    button.click()
    # # 等待confirm弹出
    WebDriverWait(driver, timeout=3).until(EC.alert_is_present())
    #
    # # 获取alert
    alert = driver.switch_to.alert
    #
    # # 获取弹出框文本
    text = alert.text
    print(text)
    time.sleep(2)
    # # 取消
    # 点击取消后 当前的alert对象就会被销毁
    alert.dismiss()
	# alert.accept() 确认
    time.sleep(2)

12. prompt box

(3) prompt box
You can also enter text;

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome() as driver:
    driver.get('https://www.w3school.com.cn/tiy/t.asp?f=js_prompt')
    driver.switch_to.frame('iframeResult')

    button = WebDriverWait(driver, timeout=3).until(
        EC.visibility_of_element_located(('xpath', '//button')))
    # 避免js没有绑定出现意外问题,加一秒延时
    time.sleep(1)
    button.click()

    # 等待alert弹出
    WebDriverWait(driver, timeout=3).until(EC.alert_is_present())

    # 获取alert
    alert = driver.switch_to.alert

    # 输入信息
    alert.send_keys('testleaf')
    time.sleep(3)
    # 确认
    alert.accept()
    # alert.dismiss() 取消
    time.sleep(3)

13. Mouse operation action chain

The mouse is executed by using the underlying interface, and the ActionChains object needs to be called to execute the corresponding method.
(1) clickAndHold: It will move to the element and then click in the middle of the given element (without releasing);
(2) contextClick: This method first moves the mouse to the position of the element, and then performs a contextual click (right-click) on the given element;
( 3) doubleClick: It will move to the element and double-click in the middle of the given element;
(4) moveToElement: This method moves the mouse to the middle of the element. When doing this, the element also scrolls into view;
(5) moveByOffset: This method moves the mouse Moves the given offset from its current position (or 0,0). If the coordinate is outside the view window, the mouse will end up outside the browser window;
(6)< /span> (7) dragAndDrop: This method first clicks and holds on the source element, then moves to the position of the target element and releases the mouse;
release

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Chrome() as driver:
    driver.get(r'file://D:\project\action.html')

    div = WebDriverWait(driver, timeout=3).until(EC.visibility_of_element_located(('xpath', '//div[@οnmοuseοver="mOver(this)"]')))
    # 移动到指定元素 move_to_element
    webdriver.ActionChains(driver).move_to_element(div).perform()
    time.sleep(2)
    # 移开多大位置x,y move_by_offset
    webdriver.ActionChains(driver).move_by_offset(xoffset=500, yoffset=500).perform()
    time.sleep(2)
    # 点住不放 click_and_hold
    div = driver.find_element_by_xpath('//div[@οnmοusedοwn="mDown(this)"]')
    webdriver.ActionChains(driver).click_and_hold(div).perform()
    time.sleep(2)
    # 松开鼠标 release
    webdriver.ActionChains(driver).release(div).perform()
    time.sleep(2)
    # double_click 双击
    button = driver.find_element_by_xpath('//button[@ondblclick]')
    webdriver.ActionChains(driver).double_click(button).perform()
    time.sleep(2)
    # drag 将div1拖拽到div2上
    div1 = driver.find_element_by_id('draggable')
    div2 = driver.find_element_by_id('droppable')
    webdriver.ActionChains(driver).drag_and_drop(div1, div2).perform()
    time.sleep(3)
    # contextClick 点击鼠标右键
    div = driver.find_element_by_xpath('//div[@οnmοusedοwn="whichButton(event)"]')
    webdriver.ActionChains(driver).context_click(div).perform()
    time.sleep(2)

14. Execute js code_scroll 1

Selenium has several methods for executing js. Here we use the most commonly used method execute_script;

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Chrome() as driver:
    driver.get('https://image.baidu.com')
    search_input = WebDriverWait(driver, 3).until(EC.visibility_of_element_located(('id', 'kw')))
    search_input.send_keys('软件测试')
    WebDriverWait(driver, 3).until(EC.element_to_be_clickable(('xpath', '//input[@value="百度一下"]'))).click()
    time.sleep(3)
    # 滚动到(0px,100px)的位置
    driver.execute_script("window.scrollTo(0,100)")
    time.sleep(1)
    driver.execute_script("window.scrollTo(0,200)")
    time.sleep(1)
    driver.execute_script("window.scrollTo(0,300)")
    time.sleep(3)
    # 移动到底部
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
    time.sleep(3)
    # 移动到顶部
    driver.execute_script("window.scrollTo(0,0)")
    time.sleep(3)

15. Execute js code_scroll 2

Selenium has several ways to execute js. Here we use the most commonly used methodexecute_script;
When executing js, you can also pass parameters to the js script. ;
The following case:
Open the page and scroll until the specified element is visible;
The following code div is passed Given arguments, they can be taken out by slicing;

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Chrome() as driver:
    driver.get(r'file://D:\project\scroll.html')
    time.sleep(2)
    div = driver.find_element_by_xpath('//div')
    # 移动到元素的底端与当前窗口的底部对齐
    driver.execute_script("arguments[0].scrollIntoView(false);", div)
    time.sleep(2)
    # 移动到元素的顶端与当前窗口的顶端对齐
    driver.execute_script("arguments[0].scrollIntoView();", div)
    time.sleep(2)

16. Upload operation_input upload

Selenium only supports the upload of input elements. You can directly use send_keys to write the absolute address of the file into the element;

import time
from selenium import webdriver

with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com')
    span = driver.find_element_by_xpath('//span[@class="soutu-btn"]')
    span.click()
    time.sleep(1)
    input = driver.find_element_by_xpath('//input[@class="upload-pic"]')

    input.send_keys(r'D:\project\find.jpg')

    # 有可能还需要提交的操作,百度是不需要
    time.sleep(10)

17. Upload operation_non-input upload_pywinauto

Many times the page does not use input to upload files, which requires other third-party packages to operate the operating system interface;
(1) pywinauto
Disadvantages: Can only be used on Windows;
Advantages: You can select multiple files, and Chinese characters are also allowed in the path;

import time

from selenium import webdriver
from pywinauto.keyboard import send_keys

with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com')
    span = driver.find_element_by_xpath('//span[@class="soutu-btn"]')
    span.click()
    time.sleep(1)
    # select_span = driver.find_element_by_xpath('//span[text()="选择文件"]')
    select_span = driver.find_element_by_xpath('//div[@class="upload-wrap"]')
    # 点击打开选择文件窗口
    select_span.click()

    time.sleep(3)
    # 选择文件
    send_keys(r'D:\project\find.jpg')
    time.sleep(1)
    # # 选择确定
    send_keys('{ENTER}')
    time.sleep(10)

18. Upload operation_non-input upload_pyautogui

Many times the page does not use input to upload files, so other third-party packages are needed to operate the operating system interface;
(2) pyautogui
Disadvantages: Only one file can be selected, and there will be problems if the file path contains Chinese characters;
Advantages: Cross-platform (windows, mac, linux);

import time

from selenium import webdriver
import pyautogui

with webdriver.Chrome() as driver:
    driver.get('https://www.baidu.com')
    span = driver.find_element_by_xpath('//span[@class="soutu-btn"]')
    span.click()
    time.sleep(1)
    # select_span = driver.find_element_by_xpath('//span[text()="选择文件"]')
    select_span = driver.find_element_by_xpath('//div[@class="upload-wrap"]')
    # 点击打开选择文件窗口
    select_span.click()

    time.sleep(3)
    # 选择文件
    pyautogui.write(r"D:\project\find.jpg")
    time.sleep(1)
    # 选择确定
    pyautogui.press('enter', 2)
    time.sleep(10)

5. Selenium adjusts window size

selenium adjusts the window to the specified size:

driver.set_window_size(900,1000)

Browser maximized:

driver.maximize_window() 

6. Determine whether the element is allowed to be operated

driver.find_element_by_name("XXX").is_enabled() # 是否可以编辑,或者按钮是否可以点击
 
driver.find_element_by_name("XXX").is_displayed() # 判断元素是否显示
 
element=driver.find_element_by_name("XXX").is_selected() # 判断元素是否选中状态

7. Get the html of a certain element

driver.find_element_by_id('XXX').get_attribute('innerHTML')

8. Other issues

1. svg positioning

Problem description:
Using the xpath method to locate elements under svg, you will find that the elements under svg cannot be located:

driver.find_element(xpath,"/html/body/div[19]/svg")

Solution 1 [Error]:
Starting from the svg element, the following elements must be written in the form of *[name()=‘svg element’]

driver.find_element(xpath,"/html/body/div[19]/*[name()='svg']/*[name()='path']")

You will find that it still cannot be located;
Solution 2 [Correct]:
If the absolute path is used, *[name()='svg'] You must add double slashes in front, otherwise the positioning will not be possible:

driver.find_element(xpath,"/html/body/div[19]//*[name()='svg']")

If is a relative path, there is no need to add a double slash before *[name()='svg'], only a single slash:

driver.find_element(xpath,"//*[@id='userinfo']/*[name()='svg']/*[name()='use']")

2. Handle chrome display notification pop-up box

When using chrome to open weibo.com, a pop-up box with the following interface will appear:
Insert image description here

This thing does not belong to the page alert pop-up box, but to the browser settings.

To turn it off, you need to configure the browser. See the script below for details:

from selenium import webdriver

options = webdriver.ChromeOptions()
prefs = {
    
    
    'profile.default_content_setting_values':{
    
    
        'notifications':2
    }
}
options.add_experimental_option('prefs',prefs)
driver = webdriver.Chrome(options = options)
driver.get("https://blog.csdn.net/testleaf/article/details/123269042")

3. Get the text of the element

element.text
get_attribute(“textContent”)
Advantages: You can get the text of hidden elements
Disadvantages: IE does not support it; get some When the text of an element is retrieved, the result contains an empty string; (have not tried it)
get_attribute("innerText")
Advantages: You can get the text of hidden elements< a i=6> Disadvantage: FireFox does not support; (Every blog you can search for mentions this shortcoming, but in actual operation, you can get the text you need every time)

4. Switch URL

Just switch directly, as follows:

driver.get('https://blog.csdn.net/testleaf/article/details/123269042')
time.sleep(2)
driver.get('https://blog.csdn.net/testleaf/article/details/123302863')

5. Find_element_by_xpath() is deprecated solution

from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome() #启动chromedriver
driver.get('http://www.baidu.com') #打开http://www.baidu.com
driver.find_element(By.XPATH,'//div[@class="detail-item-ctn"][1]').click() #点击按钮

Disclaimer:
1. This article is written to better learn the use of selenium. If it harms the interests of the relevant people, please contact us to delete it;
2. If the description in the article is incorrect, please correct it in the comments;
3. It is not easy to write the text. If you find it useful, like, favorite and follow will make the blogger very happy; < /span>https://blog.csdn.net/testleaf/article/details/123269042 This article comes from:
4. In addition, this article supports any form of reprinting. Please indicate the source for reprinting. Thank you very much! ! !

Guess you like

Origin blog.csdn.net/testleaf/article/details/123269042