Python crawler --Selenium simulated user keyboard and mouse operation

Ado, directly on the code

1, simulated keyboard

#!/usr/bin/env python 
# -*- coding:utf-8 -*-

from selenium import webdriver
 # 要想调用键盘按键操作需要引入keys包
from selenium.webdriver.common.keys import Keys
 #创建浏览器对象
driver = webdriver.Firefox()

driver.get("http://www.baidu.com") 
#打印页面标题“百度一下你就知道”
print driver.title
 #生成当前页面快照
driver.save_screenshot("baidu.png") 
# id="kw"是百度搜索框,输入字符串“微博”,跳转到搜索中国页面
driver.find_element_by_id("kw").send_keys(u"微博") 
# id="su"是百度搜索按钮,click() 是模拟点击
driver.find_element_by_id("su").click() 
# 获取新的页面快照
driver.save_screenshot(u"微博.png") 
# 打印网页渲染后的源代码
print driver.page_source 
# 获取当前页面Cookie
print driver.get_cookies() 
# ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a') 
# ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x') 
# 输入框重新输入内容
driver.find_element_by_id("kw").send_keys("test") 
# 模拟Enter回车键
driver.find_element_by_id("su").send_keys(Keys.RETURN) 
# 清除输入框内容
driver.find_element_by_id("kw").clear() 
# 生成新的页面快照
driver.save_screenshot("test.png") 
# 获取当前url
print driver.current_url 
# 关闭当前页面,如果只有一个页面,会关闭浏览器 # driver.close()

# 关闭浏览器
driver.quit()

2, mouse movements

from selenium import webdriver 
# 要想调用键盘按键操作需要引入keys包
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver import ActionChains 
#创建浏览器对象
driver = webdriver.Chrome()

driver.get("http://www.baidu.com") 

action1 = driver.find_element_by_id("su")
ActionChains(driver).move_to_element(action1).perform() #鼠标移动到某处

action2 = driver.find_element_by_id("su")
ActionChains(driver).move_to_element(action2).click(action2).perform() #鼠标移动到某处单击

action3 = driver.find_element_by_id("su")
ActionChains(driver).move_to_element(action3).double_click(action3).perform() #鼠标移动到某处双击

action4 = driver.find_element_by_id("su")
ActionChains(driver).move_to_element(action4).context_click(action4).perform()# 鼠标移动到某处右击

 3, Select Form

Needs encountered down box selection operation, Selenium specifically provided to handle the drop-down box Select class

# 导入 Select 类
from selenium.webdriver.support.ui import Select 
# 找到 name 的选项卡
select = Select(driver.find_element_by_name('status')) 
# 
select.select_by_index(1)
select.select_by_value("0")
select.select_by_visible_text(u"xxx")
  • index index from 0
  • value is the value of a property option label, not the value displayed in the drop-down box
  • visible_text option value in the label text, is a value in the drop box

 Cancel all methods:

select.deselect_all()

 

4. Processing pop

When the page appears a pop tips

alert = driver.switch_to_alert()

The page switching

A browser window will certainly be a lot, so we certainly have a ways to transfer control of the window. The method of switching window as follows:
driver.switch_to.window("this is window name")

6. The page forward and back

Operation page back and forward functions:

driver.forward()     
#前进
driver.back()        
# 后退

 

Guess you like

Origin blog.csdn.net/chang995196962/article/details/89475782