python利用selenium操作360极速浏览器

  • 查看自己360极速浏览器的内核版本:打开360极速浏览器输入:chrome://version/;查看用户代理里的chrome版本号

    查看内核版本

  • http://chromedriver.storage.googleapis.com/index.html找到最接近的版本,下载对应的chromdiriver.exe放入项目目录或系统path里

  • 启动360极速浏览器debug模式,在360极速浏览器目录打开cmd输入:
    360chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"
    端口号9222和用户数据路径可自行设置

  • 在浏览器里输入要操作的网页地址

  • python代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, options=chrome_options)
print(driver.title)
  • 如果要控制键盘或鼠标,可以像下面
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
#from selenium.webdriver import ActionChains
import time
import win32api, win32con


def getPage(p):
    pageInput.clear()
    pageInput.send_keys("{}".format(p) + Keys.RETURN)
    # driver.execute_script('$(arguments[0]).blur()', pageInput)
    # driver.execute_script('$(arguments[0]).focus()', readerImg[p + cp])
    # actionChains = ActionChains(driver)
    # actionChains.move_by_offset(500, 100)
    # actionChains.context_click().perform()
    # time.sleep(1)
    # win32con自带键位表
    # win32api.keybd_event(40,0,0,0)
    # 表示键盘方向键的下
    time.sleep(1)
    win32api.SetCursorPos((500, 200))
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
    time.sleep(1)
    win32api.keybd_event(40, win32con.KEYEVENTF_KEYUP, 0)  # 移动到菜单
    win32api.keybd_event(13, win32con.KEYEVENTF_KEYUP, 0)  # 然后enter按下
    time.sleep(3)
    # for i in str(p):
    #     win32api.keybd_event(48 + int(i), win32con.KEYEVENTF_KEYUP, 0)
    # time.sleep(1)
    win32api.keybd_event(13, win32con.KEYEVENTF_KEYUP, 0)  # 然后enter按下
    time.sleep(1)
    # actionChains.context_click()
    # actionChains.perform()
    # actionChains.send_keys(Keys.ARROW_DOWN)  # 点击键盘向下箭头
    # actionChains.perform()
    # actionChains.send_keys('v')  # 键盘输入V保存图
    # actionChains.perform()
    # 执行保存
    # actionChains.context_click().key_down(Keys.ENTER).key_up(Keys.ENTER).perform()
    # actionChains.context_click(readerImg[cp - 1 + p -1]).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
    # actionChains.context_click(pageInput).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()


chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, options=chrome_options)
print(driver.title)
pageInput = driver.find_element_by_id('pageInput')
readerImg = driver.find_elements_by_class_name('readerImg')


for k in range(100, 147):
    getPage(k)

你可能感兴趣的:(python利用selenium操作360极速浏览器)