Helium 是一款 Web 端自动化开源框架,Helium 针对 Selenium 进行了封装,它屏蔽了 Selenium 很多实现细节,提供了更加简洁直观的 API,更方便我们进行 Web 端的自动化
pip install helium
import helium
import time
helium.start_chrome()
helium.go_to("baidu.com")
time.sleep(3)
helium.kill_browser()
import helium
from selenium.webdriver import ChromeOptions
import time
import os
chrome_options = ChromeOptions()
chrome_options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
chrome_options.add_argument('--disable-blink-features=AutomationControlled') #去除浏览器navigator.webdriver
driver_path = os.path.join(os.path.dirname(os.path.dirname(os.getcwd())), 'chromedriver.exe')
helium.set_driver(driver_path)
helium.start_chrome(options=chrome_options,maximize=True)
helium.go_to("baidu.com")
time.sleep(3)
helium.kill_browser()
import helium
from selenium import webdriver
import os
import time
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度
chrome_options.add_argument('--disable-blink-features=AutomationControlled') #去除浏览器navigator.webdriver
chrome_options.add_experimental_option('useAutomationExtension', False) #去掉提示以开发者模式调用
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) #以开发者模式启动调试chrome,可以去掉提示受到自动软件控制
chrome_options.add_argument('--start-maximized') #最大化运行(全屏窗口)
driver_path = os.path.join(os.path.dirname(os.path.dirname(os.getcwd())), 'chromedriver.exe')
browser = webdriver.Chrome(executable_path=driver_path,options=chrome_options)
helium.set_driver(browser)
helium.go_to("baidu.com")
time.sleep(3)
helium.kill_browser()
window = helium.Window()
print(window.handle) #所有窗口句柄
print(window.exists()) #浏览器窗口是否存在
print(window.title) #浏览器title
window = helium.Window()
helium.switch_to(helium.find_all(window)[1])
input = helium.S('input#verifyCode') #css
input = helium.S('//input[@id="verifyCode"]') #xpath
print(input.exists()) #元素是否存在
print(input.top_left) #元素x、y坐标
print(input.width) #元素宽
print(input.height) #元素高
print(input.web_element) #返回一个web对象,selenium.webdriver.remote.webelement.WebElement
print(button.x) #元素x坐标
print(button.y) #元素y坐标
helium.write(text='test',into='请输入用户名') #根据输入框的placeholder定位元素并输入
helium.write(text='test21',into=helium.S('//input[@id="verifyCode"]'))
button = helium.Button('百度一下')
print(button.exists()) #button是否存在
print(button.width) #button宽
print(button.height) #button高
print(button.web_element) #返回一个web对象,selenium.webdriver.remote.webelement.WebElement
print(button.top_left) #元素x、y坐标
print(button.is_enabled()) #是否启用
print(button.x) #元素x坐标
print(button.y) #元素y坐标
text = helium.Text('百度一下')
print(text.exists())
print(text.top_left)
print(text.width)
print(text.height)
print(text.web_element)
print(text.x)
print(text.y)
text_field = helium.TextField()
print(text_field.exists())
print(text_field.web_element)
print(text_field.width)
print(text_field.height)
print(text_field.top_left)
print(text_field.x)
print(text_field.y)
print(text_field.is_enabled()) #是否开启
print(text_field.is_editable()) #是否编辑
link = helium.Link('新闻')
print(link.exists())
print(link.web_element)
print(link.top_left)
print(link.width)
print(link.height)
print(link.href)
print(link.x)
print(link.y)
combo_box = helium.ComboBox('下拉框')
print(combo_box.exists())
print(combo_box.web_element)
print(combo_box.options)
print(combo_box.width)
print(combo_box.height)
print(combo_box.top_left)
print(combo_box.y)
print(combo_box.x)
print(combo_box.is_editable())
check_box = helium.CheckBox('下拉框')
print(check_box.exists())
print(check_box.web_element)
print(check_box.is_enabled())
print(check_box.width)
print(check_box.height)
print(check_box.top_left)
print(check_box.y)
print(check_box.x)
image = helium.Image('下拉框')
print(image.exists())
print(image.web_element)
print(image.width)
print(image.height)
print(image.top_left)
print(image.y)
print(image.x)
alert = helium.Alert()
alert.exists()
alert.accept()
alert.dismiss()
from selenium.webdriver.common.keys import Keys
'''
NULL = Keys.NULL
CANCEL = Keys.CANCEL
HELP = Keys.HELP
BACK_SPACE = Keys.BACK_SPACE
TAB = Keys.TAB
CLEAR = Keys.CLEAR
RETURN = Keys.RETURN
ENTER = Keys.ENTER
SHIFT = Keys.SHIFT
LEFT_SHIFT = Keys.LEFT_SHIFT
CONTROL = Keys.CONTROL
LEFT_CONTROL = Keys.LEFT_CONTROL
ALT = Keys.ALT
LEFT_ALT = Keys.LEFT_ALT
PAUSE = Keys.PAUSE
ESCAPE = Keys.ESCAPE
SPACE = Keys.SPACE
PAGE_UP = Keys.PAGE_UP
PAGE_DOWN = Keys.PAGE_DOWN
END = Keys.END
HOME = Keys.HOME
LEFT = Keys.LEFT
ARROW_LEFT = Keys.ARROW_LEFT
UP = Keys.UP
ARROW_UP = Keys.ARROW_UP
RIGHT = Keys.RIGHT
ARROW_RIGHT = Keys.ARROW_RIGHT
DOWN = Keys.DOWN
ARROW_DOWN = Keys.ARROW_DOWN
INSERT = Keys.INSERT
DELETE = Keys.DELETE
SEMICOLON = Keys.SEMICOLON
EQUALS = Keys.EQUALS
NUMPAD0 = Keys.NUMPAD0
NUMPAD1 = Keys.NUMPAD1
NUMPAD2 = Keys.NUMPAD2
NUMPAD3 = Keys.NUMPAD3
NUMPAD4 = Keys.NUMPAD4
NUMPAD5 = Keys.NUMPAD5
NUMPAD6 = Keys.NUMPAD6
NUMPAD7 = Keys.NUMPAD7
NUMPAD8 = Keys.NUMPAD8
NUMPAD9 = Keys.NUMPAD9
MULTIPLY = Keys.MULTIPLY
ADD = Keys.ADD
SEPARATOR = Keys.SEPARATOR
SUBTRACT = Keys.SUBTRACT
DECIMAL = Keys.DECIMAL
DIVIDE = Keys.DIVIDE
F1 = Keys.F1
F2 = Keys.F2
F3 = Keys.F3
F4 = Keys.F4
F5 = Keys.F5
F6 = Keys.F6
F7 = Keys.F7
F8 = Keys.F8
F9 = Keys.F9
F10 = Keys.F10
F11 = Keys.F11
F12 = Keys.F12
META = Keys.META
COMMAND = Keys.COMMAND
'''
helium.press(Keys.ENTER)