Selenium基础教程

1.Selenium环境安装

1.1 浏览器安装

Chrome和ChromeDriver下载地址:  https://googlechromelabs.github.io/chrome-for-testing/

注意:驱动版本号要和浏览器版本号一致;

安装后关闭浏览器自动更新: services.msc:打开系统服务找到和 google 相关的服务,全部修改为禁用

1.2 安装第三方库selenium

pip  install  selenium

2.创建浏览器设置、打开

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options   # 用于设置浏览器
from selenium.webdriver.chrome.service import Service


# 创建设置浏览器的对象
q1 = Options()

# 禁用沙盒模式
q1.add_argument('--no-sandbox')

# 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
q1.add_experimental_option(name='detach', value=True)

# 创建并启动浏览器
a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)

3.打开网页、关闭网页、关闭浏览器

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='http://www.baidu.com/')

time.sleep(3)
# 关闭当前标签页,get打开的网址
a1.close()

# 退出浏览器,释放浏览器驱动
a1.quit()

4.浏览器的最大化和最小化

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

time.sleep(2)
# 浏览器最小化
a1.minimize_window()

time.sleep(2)
# 浏览器最大化
a1.maximize_window()


5.浏览器的打开位置和尺寸

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# 浏览器打开位置
a1.set_window_position(20, 20)

# 浏览器窗口尺寸
a1.set_window_size(600, 800)

time.sleep(2)
a1.set_window_rect(10, 10, 800, 1000)

6.浏览器截图和刷新当前网页

a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

a1.maximize_window()

# 浏览器截图,保存
a1.get_screenshot_as_file('1.png')

time.sleep(3)

# 刷新网页
a1.refresh()

7.元素定位

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# 定位一个元素,找到会返回结果,找不到会报错
el = a1.find_element(By.NAME, 'j_username')
print(el)

# el = a1.find_element(By.CLASS_NAME, 'lui_login_input_username')
# print(el)

# 定位多个元素(找到返回列表, 找不到返回空列表)
els = a1.find_elements(By.NAME, 'j_password')
print(els)

# 浏览器查找多个元素(F12 控制台查找)
# document.getElementsByName('j_username')
# document.getElementsByName('j_password')

8.元素交互操作

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# 定位一个元素,找到会返回结果,找不到会报错
username = a1.find_element(By.NAME, 'j_username')
print(username)

password = a1.find_element(By.NAME, 'j_password')
print(password)

btn_login = a1.find_element(By.CLASS_NAME, 'lui_login_button_div_c')
print(btn_login)

# 元素清空
username.clear()
password.clear()

# 元素输入(输入登录用户名和密码)
username.send_keys("xxxxxx")
password.send_keys("xxxxxx")

# 元素点击(点击登录按钮)
btn_login.click()

9.元素定位ID

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1

a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# 通过ID查找元素,并获取元素的属性 get_property 和 get_attribute 获取的结果是一样的
# 
# id="login-bgImg" alt="" style="height: 690px; width: 1226.67px; left: -123.5px; top: 0px;">
bgImg = a1.find_element(By.ID, 'login-bgImg')
print(bgImg.get_attribute('src'))   # https://xxxxxx.com/resource/customization/images/login_single_random/single_random_login_bg1.jpg?s_cache=348091741757
print(bgImg.get_dom_attribute('src'))   # /resource/customization/images/login_single_random/single_random_login_bg1.jpg?s_cache=348091741757
print(bgImg.get_property('src'))    # https://xxxxxx.com/resource/customization/images/login_single_random/single_random_login_bg1.jpg?s_cache=348091741757
print(bgImg.get_property('style'))  # ['height', 'width', 'left', 'top']
print(bgImg.get_property('height'))  # 1080
print(bgImg.get_attribute('height'))    # 1080

10.元素定位-NAME

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# 

el = a1.find_element(By.NAME, 'j_password')
el.send_keys('12345678')
print(el.get_property('type'))
print(el.get_attribute('type'))

11.元素定位-CLASS_NAME

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# 
# 注意:CLASS_NAME 定位的value值不能有空格
el = a1.find_element(By.CLASS_NAME, 'lui_login_input_password')
el.send_keys('12345678')
print(el.get_property('type'))
print(el.get_attribute('type'))

12.元素定位-TAG_NAME

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# TAG_NAME 按标签名字查找;元素特别多,需要切片处理

el = a1.find_elements(By.TAG_NAME, 'input')
for item in el:
    print(item.get_property('name'))

13.元素定位-LINK_TEXT和PARTIAL_LINK_TEXT

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# LINK_TEXT 按连接文本 (

# 苏ICP备12002088号-4

# 精准定位
# el = a1.find_element(By.LINK_TEXT, '苏ICP备12002088号-4')

# 模糊文本定位
el = a1.find_element(By.PARTIAL_LINK_TEXT, '苏ICP备')

print(el.get_property('href'))  # https://beian.miit.gov.cn/

14.元素定位-CSS_SELECTOR

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# CSS_SELECTOR 按连接文本 (
# 1. #id            =  #号 + id值            通过id定位
# 2. .class         =  点  + class值         通过class定位
# 3. 不加修饰符       =  标签头                通过标签头定位
# 4. 任意类型        =  "[类型='精准值']"      通过任意类型定位
# 5. 任意类型        =  "[类型*='模糊值']"      通过任意类型定位(模糊值)
# 6. 任意类型        =  "[类型^='开头值']"      通过任意类型定位(开头值)
# 7. 任意类型        =  "[类型$='结尾值']"      通过任意类型定位(结尾值)
# 以上方法都属于理论定位方法

# 8. 在谷歌控制台直接复制 Selector (唯一);个别元素的selector会比较长

# 
# id="login-bgImg" alt="" style="height: 1080px; width: 1920px; left: 0px; top: -324px;">

el1 = a1.find_element(By.CSS_SELECTOR, '#login-bgImg')
print(el1.get_property('src'))

el8 = a1.find_element(By.CSS_SELECTOR, '#login-bgImg')   # selector
print(el8.get_property('src'))

# 
# onfocus="this.select();" value="" placeholder="用户名" autocomplete="off">

el2 = a1.find_element(By.CSS_SELECTOR, '.lui_login_input_username')
print(el2.get_attribute('name'))

el3 = a1.find_elements(By.CSS_SELECTOR, 'img')
print(el3)

el4 = a1.find_element(By.CSS_SELECTOR, "[name='j_password']")
print(el4.get_property('name'))

el5 = a1.find_element(By.CSS_SELECTOR, "[name*='j_user']")
print(el5.get_property('name'))

el6 = a1.find_elements(By.CSS_SELECTOR, "[name^=j_]")
for item in el6:
    print('EL6:', end='')
    print(item.get_property('name'))

el7 = a1.find_element(By.CSS_SELECTOR, "[name$=word]")
print('EL7:', end='')
print(el7.get_property('name'))

15.元素定位-XPATH

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

# 浏览器复制xpath(属性+路径)
# 浏览器复制完整Xpath (可以精准定位,缺点是路径比较长)

el = a1.find_element(By.XPATH, '/html/body/div[1]/div[3]/div[1]/div[2]/div/form/table/tbody/tr[2]/td[2]/div/input')
print(el.get_attribute('name'))

16.元素定位练习

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)

    # 元素定位隐形等待(10s内找到定位元素,没找到报错)
    a1.implicitly_wait(10)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

a1.maximize_window()

# 输入账号
a1.find_element(By.XPATH, '/html/body/div[1]/div[3]/div[1]/div[2]/div/form/table/tbody/tr[2]/td[2]/div/input')\
    .send_keys('xxxxxx')
# 输入密码
a1.find_element(By.XPATH, '/html/body/div[1]/div[3]/div[1]/div[2]/div/form/table/tbody/tr[3]/td[2]/div/input')\
    .send_keys('xxxxxx')
# 点击登录
a1.find_element(By.XPATH, '/html/body/div[1]/div[3]/div[1]/div[2]/div/form/table/tbody/tr[5]/td/a/div/div/div').click()

# 
# id="sys_person_userpic_img">

el = a1.find_element(By.ID, 'sys_person_userpic_img')
if el:
    print(el.get_attribute('src'))

# 
# 常用查询
# 点击常用查询 el = a1.find_element(By.XPATH, '//*[@id="p_bca6a2d1bb12dc30c63a"]/div/div[2]/div/div/div[2]/div/div/span') if el: el.click() # 点击定制数据报表 el = a1.find_element(By.CSS_SELECTOR, '#lui-id-66 > ul > li:nth-child(1) > a') if el: el.click()

17.获取句柄、切换标签页

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)

    # 元素定位隐形等待(10s内找到定位元素,没找到报错)
    a1.implicitly_wait(10)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='https://xxxxxx.com/')

a1.maximize_window()

# 输入账号
a1.find_element(By.XPATH, '/html/body/div[1]/div[3]/div[1]/div[2]/div/form/table/tbody/tr[2]/td[2]/div/input')\
    .send_keys('xxxxxx')
# 输入密码
a1.find_element(By.XPATH, '/html/body/div[1]/div[3]/div[1]/div[2]/div/form/table/tbody/tr[3]/td[2]/div/input')\
    .send_keys('xxxxxx')
# 点击登录
a1.find_element(By.XPATH, '/html/body/div[1]/div[3]/div[1]/div[2]/div/form/table/tbody/tr[5]/td/a/div/div/div').click()

# 点击携物外出
el = a1.find_element(By.XPATH, '/html/body/div[4]/div[1]/table[2]/tbody/tr/td[3]/div/div/div[3]/div/div/div[1]/div/div[1]/div/div/ul/li[5]/a')
if el:
    el.click()

# 获取全部标签页句柄
all_handles = a1.window_handles
print(all_handles)

# 关闭之前标签页
a1.close()

# 切换到新打开的标签页
a1.switch_to.window(all_handles[1])

# 勾选携出

a1.find_element(By.XPATH, '/html/body/table/tbody/tr/td[1]/div/form/div[2]/div/div[1]/div[1]/div[2]/div/div/div/div[1]'
                          '/div/div/div/xformflag[1]/table/tbody/tr[3]/td[2]/div/xformflag/select/option[2]').click()

# 常用意见选择:同意
a1.find_element(By.XPATH, '/html/body/table/tbody/tr/td[1]/div/form/div[2]/div/div[1]/div[3]/div[2]/div/div/div/div[1]'
                          '/div/div[2]/table/tbody/tr[10]/td[2]/table/tbody/tr[1]/td/select/option[2]').click()

# 携出日期
a1.find_element(By.XPATH, '/html/body/table/tbody/tr/td[1]/div/form/div[2]/div/div[1]/div[1]/div[2]/div/div/div/div[1]'
                          '/div/div/div/xformflag[3]/div/table/tbody/tr[3]/td[2]/label/xformflag/div/div[1]/input')\
    .send_keys('2025-07-01')

# 携出目的 出差
a1.find_element(By.XPATH, '/html/body/table/tbody/tr/td[1]/div/form/div[2]/div/div[1]/div[1]/div[2]/div/div/div/div[1]'
                          '/div/div/div/xformflag[3]/div/table/tbody/tr[1]/td[2]/div/xformflag/label[1]/span').click()

# 携出地点 深圳
a1.find_element(By.XPATH, '/html/body/table/tbody/tr/td[1]/div/form/div[2]/div/div[1]/div[1]/div[2]/div/div/div/div[1]'
                          '/div/div/div/xformflag[3]/div/table/tbody/tr[4]/td[2]/div[1]/xformflag/select/option[2]')\
    .click()

# 携出原因
a1.find_element(By.XPATH, '/html/body/table/tbody/tr/td[1]/div/form/div[2]/div/div[1]/div[1]/div[2]/div/div/div/div[1]'
                          '/div/div/div/xformflag[3]/div/table/tbody/tr[4]/td[4]/div[1]/xformflag/input')\
    .send_keys('出差办公')

18.alert弹出框

前端代码:






测试代码:

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)

    # 元素定位隐形等待(10s内找到定位元素,没找到报错)
    a1.implicitly_wait(10)
    return a1

a1 = setUp()

# 打开指定网址
a1.get(url='http://localhost:9000/')

el = a1.find_element(By.XPATH, '//*[@id="app"]/button/span')
el.click()

time.sleep(2)

# 获取弹窗的提示内容
print(a1.switch_to.alert.text)

# 点击确认
# a1.switch_to.alert.accept()

# 点击取消
a1.switch_to.alert.dismiss()

19.confirm弹出框

前端代码:






测试代码:

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)

    # 元素定位隐形等待(10s内找到定位元素,没找到报错)
    a1.implicitly_wait(10)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='http://localhost:9000/')

el = a1.find_element(By.XPATH, '//*[@id="app"]/button/span')
el.click()

time.sleep(2)

# 获取弹窗的提示内容
print(a1.switch_to.alert.text)

20.提示框(prompt)元素交互

前端代码:






测试代码:

from selenium import webdriver  # 用于操作浏览器
from selenium.webdriver.chrome.options import Options  # 用于设置浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time


def setUp():
    # 创建设置浏览器的对象
    q1 = Options()

    # 禁用沙盒模式(增加兼容性)
    q1.add_argument('--no-sandbox')

    # 保持浏览器打开状态(默认代码执行完毕,浏览器会关闭)
    q1.add_experimental_option(name='detach', value=True)

    # 创建并启动浏览器
    a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)

    # 元素定位隐形等待(10s内找到定位元素,没找到报错)
    a1.implicitly_wait(10)
    return a1


a1 = setUp()

# 打开指定网址
a1.get(url='http://localhost:9000/')

el = a1.find_element(By.XPATH, '//*[@id="app"]/button/span')
el.click()

time.sleep(2)

# 获取弹窗的提示内容
print(a1.switch_to.alert.text)

# 弹窗输入内容
a1.switch_to.alert.send_keys('alex lei')

# 点击确认
a1.switch_to.alert.accept()

# 点击取消
# a1.switch_to.alert.dismiss()

你可能感兴趣的:(selenium,python,测试工具)