python之selenium

1、安裝

一、selenium

pip install selenium

二、驅動

 (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加入驅動路徑css

 

2、簡單實用

一、執行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

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()	#退出

  

三、獲取元素

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.htmlhtml

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

四、一些方法和其餘

(1)Selenium:利用select模塊處理下拉框

from selenium.webdriver.support.select import Select

select_by_index # 經過索引定位git

select_by_value # 經過value值定位github

select_by_visible_text # 經過文本值定位web

根據索引選擇 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_index("3")chrome

根據value值選擇 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_value("3線")api

根據文本值選擇 Select(driver.find_element_by_name("storeDeclare.cityLine")).select_by_visible_text("3線")瀏覽器

refer:cookie

 

(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()

五、等待

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

六、api

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

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

相關文章
相關標籤/搜索