• Web界面自动化操作工具 - Selenium常见用法


    Selenium是一个用于自动化浏览器操作的工具,常用于Web应用程序的测试和爬虫开发。

    下面是一些Python Selenium的常见用法和代码示例:

    1. 导入Selenium库和WebDriver:

    from selenium import webdriver
    
    • 1

    2. 创建WebDriver实例:

    # 使用Chrome浏览器
    driver = webdriver.Chrome()
    
    # 使用Firefox浏览器
    driver = webdriver.Firefox()
    
    # 使用Edge浏览器
    driver = webdriver.Edge()
    
    # 使用Safari浏览器
    driver = webdriver.Safari()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3. 打开URL并获取页面内容:

    # 打开URL
    driver.get("https://www.example.com")
    
    # 获取页面标题
    title = driver.title
    print("页面标题:", title)
    
    # 获取页面源代码
    page_source = driver.page_source
    print("页面源代码:", page_source)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    4. 查找元素并进行操作:

    # 通过ID查找元素并输入文本
    element = driver.find_element_by_id("username")
    element.send_keys("myusername")
    
    # 通过XPath查找元素并点击
    element = driver.find_element_by_xpath("//button[@class='submit']")
    element.click()
    
    # 通过CSS选择器查找元素并获取文本
    element = driver.find_element_by_css_selector(".message")
    text = element.text
    print("元素文本:", text)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    其他多种Selenuim定位元素的方法:

    4.1. 通过名称定位:

    element = driver.find_element_by_name("element_name")
    
    • 1

    4.2. 通过类名定位:

    element = driver.find_element_by_class_name("element_class")
    
    • 1

    4.3 通过标签名定位:

    element = driver.find_element_by_tag_name("element_tag")
    
    • 1

    4.4 通过链接文本定位:

    element = driver.find_element_by_link_text("link_text")
    
    • 1

    4.5. 通过部分链接文本定位:

    element = driver.find_element_by_partial_link_text("partial_link_text")
    
    • 1

    5. 处理弹窗和框架:

    # 切换到弹窗
    alert = driver.switch_to.alert
    alert.accept()  # 接受弹窗
    
    # 切换到框架
    frame = driver.switch_to.frame("frame_name")
    # 在框架中执行其他操作
    driver.switch_to.default_content()  # 切回默认框架
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    6. 执行JavaScript代码:

    # 执行JavaScript脚本
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")  # 滚动到页面底部
    
    • 1
    • 2

    7. 等待页面加载和元素可见:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    # 等待页面加载完成
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.presence_of_element_located((By.ID, "username")))
    
    # 等待元素可见并点击
    element = wait.until(EC.visibility_of_element_located((By.XPATH, "//button[@class='submit']")))
    element.click()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    8. 关闭浏览器:

    # 关闭当前窗口
    driver.close()
    
    # 关闭整个浏览器
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这些是Python Selenium的一些常见用法和代码示例。Selenium提供了丰富的API用于模拟用户操作和获取页面内容,可以根据具体的需求进行更多的操作和定制化开发。

    9.使用场景1 - 打开百度并搜索

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    # 启动Chrome浏览器
    driver = webdriver.Chrome()
    
    # 打开百度网页
    driver.get("http://www.baidu.com")
    
    # 找到搜索输入框并输入关键词
    search_input = driver.find_element(By.ID, "kw")
    search_input.send_keys("selenium")
    
    # 提交搜索表单
    search_input.submit()
    
    # 等待搜索结果加载完成
    driver.implicitly_wait(20)
    
    # 找到搜索结果列表中的第一个结果
    first_result = driver.find_element(By.CSS_SELECTOR, "#content_left .result h3 a")
    
    # 获取第一个结果的文本内容
    first_result_text = first_result.text
    
    # 打印第一个搜索结果
    print(first_result_text)
    
    # 关闭浏览器
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    10.使用场景2 - 持续集成Jenkins触发

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # 启动Chrome浏览器
    driver = webdriver.Chrome()
    
    # 打开Jenkins登录页面
    driver.get("http://Jenkins_Loginpage/")
    
    driver.implicitly_wait(10)
    
    # 找到用户名和密码输入框,并输入相应的值
    username_input = driver.find_element(By.ID, "j_username")
    password_input = driver.find_element(By.NAME, "j_password")
    
    username_input.send_keys("username")
    password_input.send_keys("password")
    
    # 提交登录表单
    login_button = driver.find_element(By.NAME, "Submit")
    login_button.click()
    
    # 等待登录成功后的页面加载完成
    driver.implicitly_wait(5)
    
    # 打开Jenkins首页
    driver.get("http://Jenkins_Homepage/")
    
    # 找到搜索输入框,并输入任务名称
    search_input = driver.find_element(By.ID, "search-box")
    search_input.send_keys("Job_Name")
    
    # 提交搜索表单
    search_input.submit()
    
    # 等待搜索结果加载完成
    driver.implicitly_wait(5)
    
    # 找到搜索结果中的任务链接,并点击进入任务详情页
    job_link = driver.find_element(By.LINK_TEXT, "Job_Name")
    job_link.click()
    
    # 找到构建按钮,并点击进行构建
    build_button = driver.find_element(By.XPATH, "//*[@id='tasks']/div[4]/span")
    
    build_button.click()
    
    # 等待构建成功的状态出现
    wait = WebDriverWait(driver, 60)
    build_success_status = wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, ".build-status"), "Success"))
    
    # 获取构建状态文本内容
    build_status = driver.find_element(By.CSS_SELECTOR, ".build-status").text
    
    # 打印构建状态
    print(build_status)
    
    # 关闭浏览器
    driver.quit()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    Vuex的核心概念及作用
    2023 龙蜥操作系统大会演讲实录:《兼容龙蜥的云原生大模型数据计算系统——πDataCS》
    Java面试笔试题大汇总一(最全+详细答案)
    Rust 添加老版本的依赖时报错
    一文带你入门 Java 函数式编程
    Vue3 实现路由vue-router效果
    【云原生进阶之PaaS中间件】第一章Redis-2.4缓存更新机制
    JAVA计算机毕业设计宠物销售管理系统Mybatis+系统+数据库+调试部署
    【python】直方图正则化详解和示例
    Electron桌面应用开发基础
  • 原文地址:https://blog.csdn.net/holyvslin/article/details/133989831