python之selenium

一、安装

1、selenium

pip install selenium

2、驱动

 (1)下载驱动

https://sites.google.com/a/chromium.org/chromedriver/downloads

https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

https://github.com/mozilla/geckodriver/releases

https://webkit.org/blog/6900/webdriver-support-in-safari-10/

(2)配置环境

  下载好对应版本驱动,配置环境PATH(Windows),PATH加入驱动路径

 

二、简单实用

1、执行js,打开浏览器窗口

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://8.8.8.8/")


for i in range(100):
	js="window.open('http://1.1.1.%s')" % str(i)
	driver.execute_script(js)

#driver.quit()

  

2、简单使用2

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()	#清空自动填入的值,保证我们的输入
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()	#退出

  

3、获取元素

1.id定位:find_element_by_id(self, id_)
2.name定位:find_element_by_name(self, name)
3.class定位:find_element_by_class_name(self, name)
4.tag定位:find_element_by_tag_name(self, name)
5.link定位:find_element_by_link_text(self, link_text)
6.partial_link定位find_element_by_partial_link_text(self, link_text)
7.xpath定位:find_element_by_xpath(self, xpath)
8.css定位:find_element_by_css_selector(self, css_selector)
9.id复数定位find_elements_by_id(self, id_)
10.name复数定位find_elements_by_name(self, name)
11.class复数定位find_elements_by_class_name(self, name)
12.tag复数定位find_elements_by_tag_name(self, name)
13.link复数定位find_elements_by_link_text(self, text)
14.partial_link复数定位find_elements_by_partial_link_text(self, link_text)
15.xpath复数定位find_elements_by_xpath(self, xpath)
16.css复数定位find_elements_by_css_selector(self, css_selector

  

refer:https://www.cnblogs.com/yoyoketang/p/6557421.html

https://selenium-python.readthedocs.io/locating-elements.html

4、一些方法和其他

(1)Selenium:利用select模块处理下拉框

from selenium.webdriver.support.select import Select

select_by_index # 通过索引定位

select_by_value # 通过value值定位

select_by_visible_text # 通过文本值定位

根据索引选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_index("3")

根据value值选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_value("3线")

根据文本值选择 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_visible_text("3线")

refer:

 

(2)selenium中sendkeys()方法输入中文报错之解决方案

#方式1,在中文前加入u 
# driver.find_element_by_class_name(“s_ipt”).send_keys(u’测试’)

#方式2,使用decode()方法 
str = ‘测试’ 
print str 
driver.find_element_by_class_name(“s_ipt”).send_keys(str.decode(‘utf-8’)) 

(3)获取当前页面信息

记得延时,不然有可能出错

driver.title  #标题

driver.current_url  #链接

 

(4)输入

element.send_keys("some text")

(5)点击

driver.find_element_by_id("submit").click()

  

(6)前进后退

driver.forward()
driver.back()

  

(7)Cookies

cookie = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
driver.add_cookie(cookie)
driver.get_cookies()

5、等待

https://selenium-python.readthedocs.io/waits.html

6、api

https://selenium-python.readthedocs.io/api.html

https://seleniumhq.github.io/selenium/docs/api/py/api.html

 

posted @ 2019-03-22 17:23  nobgr  阅读(241)  评论(0编辑  收藏  举报