python打开selenium

from selenium import webdriver
browser = webdriver.Firefox()
baseurl=\'http://d.longtugame.com\'
browser.get(url)

webdriver 重要属性

#页面源代码
br.page_source
#当前网页地址
br.current_url


webdriver方法全面备忘

搜索网页元素

#通过xpath查找
name=browser.find_element_by_xpath(\'/html/body/div/div[1]\')

#通过class name查找
#注意返回的不是一个值,是个数组
suipian=browser.find_elements_by_class_name(\'the_des\')
myhuoqu=(suipian[0].find_elements_by_tag_name(\'p\'))[0].get_attribute(\'innerHTML\').encode(\"utf-8\")

#通过id方式定位
browser.find_element_by_id(\"kw\").send_keys(\"selenium\")

#通过name方式定位
browser.find_element_by_name(\"wd\").send_keys(\"selenium\")

#通过tag name方式定位
browser.find_element_by_tag_name(\"input\").send_keys(\"selenium\")

#通过class name 方式定位
browser.find_element_by_class_name(\"s_ipt\").send_keys(\"selenium\")

#通过CSS方式定位
#TMD css selector不是很懂,以后有空在学
#有可能也是返回数组
botton=browser.find_element_by_css_selector(\"#kw\")
checkboxes = dr.find_elements_by_css_selector(\'input[type=checkbox]\')
#通过链接文字定位
browser.find_element_by_link_text(\"贴 吧\").click()
#通过链接的部分文字定位
browser.find_element_by_partial_link_text(\"贴\").click()

获取元素属性

#获得该元素的文本
browser.find_element_by_partial_link_text(\"贴\").text

动作模拟

引入包

from selenium.webdriver.common.action_chains import ActionChains

各种操作

#鼠标定位到子元素上
webdriver.ActionChains(driver).move_to_element(menu).perform()

#定位到要右击的元素
qqq =driver.find_element_by_xpath(\"/html/body/div/div[2]/div[2]/div/div[3]\")
#对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(qqq).perform()

#定位到要双击的元素
qqq =driver.find_element_by_xpath(\"xxx\")
#对定位到的元素执行鼠标双击操作
ActionChains(driver).double_click(qqq).perform()

#定位元素的原位置
element = driver.find_element_by_name(\"source\")
#定位元素要移动到的目标位置
target =  driver.find_element_by_name(\"target\")
#执行元素的移动操作
ActionChains(driver).drag_and_drop(element, target).perform()

#点击元素
ActionChains(driver).click(元素)

#在指定元素上按下鼠标不释放
ActionChains(driver).click_and_hold(元素)
#在指定元素上释放鼠标
ActionChains(driver).release(on_element)

#双击元素
ActionChains(driver).double_click(元素)

#发送一个键给元素
#按下一个键,不松手,主要用来处里按下control,Shift,Alt键用
#key:The modifier key to send. Values are defined in Keys class.
ActionChains(driver).key_down(key,元素)

#Releases a modifier key.
#key:The modifier key to send. Values are defined in Keys class.
#element:The element to send keys. If None, sends a key to current focused element.
ActionChains(driver).key_up(key, 元素)

#移动鼠标
ActionChains(driver).move_by_offset(xoffset, yoffset)

#基于某一元素移动鼠标
#Offsets are relative to the top-left corner of the element.
ActionChains(driver).move_to_element_with_offset(to_element, xoffset, yoffset)

#发送一堆键到当前元素
ActionChains(driver).send_keys(*keys_to_send)

#发送一堆键到指定元素
ActionChains(driver).send_keys_to_element(self, element,*keys_to_send)

元素相关操作说明

WebElement的方法:一般来说,所有有趣的操作与页面进行交互的有趣的操作,都通过 WebElement 完成

当前元素的ID

id

获取元素标签名的属性

tag_name

获取该元素的文本。

text

单击(点击)元素

click()

提交表单

submit()

清除一个文本输入元素的文本

clear()

获得属性值

get_attribute(name)

元素是否被选择

s_selected(self)

元素是否被启用

is_enabled()

查找元素的id

find_element_by_id(id_)
find_elements_by_id(id_)

查找元素的name

find_element_by_name(name)
find_elements_by_name(name)

查找元素的链接文本

find_element_by_link_text(link_text)
find_elements_by_link_text(link_text)

查找元素的链接的部分文本

find_element_by_partial_link_text(link_text)
find_elements_by_partial_link_text(link_text)

查找元素的标签名

find_element_by_tag_name(name)
find_elements_by_tag_name(name)

查找元素的xpath

find_element_by_xpath(xpath)

查找元素内的子元素的xpath

find_elements_by_xpath(xpath)

查找一个元素的类名

find_element_by_class_name(name)

查找元素的类名

find_elements_by_class_name(name)

查找并返回一个元素的CSS 选择器

find_element_by_css_selector(css_selector)

查找并返回多个元素的CSS 选择器列表

find_elements_by_css_selector(css_selector)

模拟输入元素

send_keys(*value)

错误备忘

在执行webdriver.get(url)的时候报f.QueryInterface is not a function
通常是因为没有添加http://或者https://就是没有添加网络协议的原因
The get method requires a full URL. That is, the URI including the protocol and hostname/ip.