selenium概述 什么是selenium? Selenium是一个广泛使用的Web应用程序测试工具。它通过模拟真实用户在浏览器中的操作来测试Web应用,支持多种现代Web浏览器,如IE、Mozilla Firefox、Google Chrome、Safari、Opera和Edge等 3 。
Selenium的核心功能包括:
浏览器兼容性测试 :测试应用程序在不同浏览器和操作系统上的兼容性。
系统功能测试 :创建回归测试来检验软件功能和用户需求。
支持多种编程语言 :测试脚本可以使用Java、Python等多种语言编写。
Selenium的主要组件包括:
Selenium IDE :一个Firefox插件,可以录制用户的基本操作,生成测试用例,并可以将这些测试用例转换为其他语言的自动化脚本。
**Selenium Remote Control (RC)**:支持多种平台和浏览器,可以用多种语言编写测试用例。
Selenium Grid :允许Selenium-RC针对大规模的测试案例集或需要在不同环境中运行的测试案例集进行扩展。
对于初学者,Selenium的入门步骤通常包括安装Selenium库、配置浏览器驱动程序,以及学习如何编写和执行Selenium脚本。例如,使用Python进行Selenium自动化测试时,需要先安装selenium库和对应的浏览器驱动程序,然后通过编写Python脚本来控制浏览器行为,如打开网页、模拟鼠标点击、键盘输入等操作。
selenium入门
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 from selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.select import Selectimport time chrome_driver_path = r".\chromedriver-win64\chromedriver.exe" service = Service(chrome_driver_path) driver = webdriver.Chrome(service=service) url = "https://www.lagou.com" driver.get(url)print (driver.title) search_input=driver.find_element("xpath" , '//*[@id="search_input"]' ) search_input.send_keys("python" ) time.sleep(1 ) search_input.send_keys(Keys.ENTER) xxx_list=driver.find_element("xpath" , '对应的xpath路径' )for xxx in xxx_list: xxx.find_element("xpath" , '对应的xpath路径' ) driver.execute_script(''' var a=document.getElementsByClassName('un-login-banner')[0]; a.parentNode.removeChild(a); ''' ) driver.switch_to.window(driver.window_handles[-1 ]) job_detail=driver.find_element("xpath" , '//*[@id="job_detail"]' ) txt=job_detail.text driver.close() driver.switch_to.window(driver.window_handles[0 ]) time.sleep(10000000 ) iframe=driver.find_element("xpath" , 'iframe的xpath路径' ) driver.switch_to.frame(iframe)input =driver.find_element("xpath" , '某标签的xpath路径' ) placeholder=input .get_property("placeholder" ) driver.switch_to.parent_frame()''' <select> <option value="1">选项1</option> <option value="2">选项2</option> <option value="3">选项3</option> </select> ''' sel=driver.find_element("xpath" , 'select的xpath路径' ) select=Select(sel) options=select.options select.select_by_index(0 ) select.select_by_value("2" ) select.select_by_visible_text("选项2" ) pagesource=driver.page_sourcefrom selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless' ) options.add_argument('--disable-gpu' ) driver = webdriver.Chrome(options=options) png = driver.find_element("xpath" , '图片的xpath路径' ).screenshot_as_png
selenium识别验证码
方法一:通过调用超级鹰网站的api实现验证码识别(需要付费购买服务)
方法二:通过调用图鉴网站的api实现验证码识别(需要付费购买服务)
注:滑块验证难以识别