一、環境部署
python+selenium+pycharm+webdriver 1、 python安裝包:https://www.python.org/getit/ 2、 PyCharm 安裝包:http://www.jetbrains.com/pycharm/download/ 3、 Selenium安裝包:https://pypi.python.org/pypi/selenium selenium安裝方式一: 安裝python包,選擇全部組件(pip、安裝過程中配置環境變量) 然后執行pip install -U selenium 聯網安裝Selenium; 安裝方式二: 安裝python包,選擇全部組件(pip、安裝過程中配置環境變量) 解壓selenium-3.13.0.tar.gz,然后用cmd進入解壓目錄,使用命令 Python setup.py install 安裝Selenium。 4、 webdriver配置(以chromedriver為例): Chromedriver下載地址:http://npm.taobao.org/mirrors/chromedriver/ ***版本與瀏覽器版本保持一致 配置方式一: 1)把下載好的chromedriver.exe程序放置到python的安裝路徑下 2)在python中代碼編寫如下即可: driver = webdriver.Chrome() # Firefox、Ie、Edge等 配置方式二: 1)把下載好的chromedriver.exe程序放置到python項目中(其它路徑也可) 2)在python中代碼編寫如下即可: chromePath = chromedriver.exe路徑 os.environ[‘webdriver.chrome.driver’] = chromePath # gecko ie等 driver = webdriver.Chrome(executable_path=chromePath) # Firefox、Ie等 很長時間不用最好將本地的瀏覽器升級到最新版本
二、注意事項
1、把解壓的webdriver放置到python根路徑 ***在pycharm中創建目錄存放webdriver 2、若安裝的瀏覽器是火狐,則需安裝在默認路徑,否則易報錯 3、寫代碼時,1)需從selenium中導入webdriver eg:from selenium import webdriver 2)需定義webdriver文件路徑 chrome_driver_path=os.path.join(current,’..chromedriver文件存放的路徑') 3)打開chrome瀏覽器語句 driver = webdriver.Chrome(executable_path=chrome_driver_path) 4)輸入網址 driver.get(http://www.baidu.com)
三、識別元素
1. 基本元素定位
driver.find_element_by_id('id_value').send_keys('最新疫情咨訊') #通過id定位 driver.find_element_by_name('name_value').send_keys('最新疫情咨訊') #通過name定位 driver.find_element_by_class_name('class_name_value').send_keys('最新疫情信息') #通過class元素定位 driver.find_element_by_tag_name('input').send_key('最新疫情咨訊') #通過tag_name定位,<缺點:tag_name值不唯一> driver.find_element_by_link_text('新聞').click() #link_text鏈接中的文本 <缺點:只對<a>標簽生效> driver.find_element_by_partial_link_text('新').click() #partial_link_text部分鏈接中的文本 <缺點:只對<a>標簽生效>
2.xpath定位元素
driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/form/span[1]/input') #絕對路徑,單反斜線,遇到同層級多個元素,用下標從1開始,自頂向下寫 driver.find_element_by_xpath('form/span[1]/input') #相對路徑,雙反斜線,遇到同層級多個元素,用下標從1開始,自底向上寫 #屬性定位 // 標簽名[屬性名=‘‘屬性值’’] driver.find_element_by_xpath('//input[@id=''kw'']').send_keys('新夢想') #多屬性定位 and or driver.find_element_by_xpath('//input[@id=''kw''] and @maxlength=''225'']').send_keys('新夢想軟測') #支持通配符 * driver.find_element_by_xpath('//*[@id=''kw'' ]').send_keys('新夢想軟測') #模糊定位,稱為部分屬性值定位 #字符串以特定值開頭 //a[start-with(@參數1,‘‘參數值2’’)] #以什么結尾 ends-with() #有的會報錯,原因, #包含 contain() 應用的場景(1、元素信息過長 2、動態屬性元素,eg:始終點擊第一個訂單,即動態獲取第一個訂單號編碼) #元素文本定位 text() link_text只能用於a標簽,局限性比較大,而xpath所有標簽均支持 driver.find_element_by_xpath('//a[text()=''新聞'']').click()
3.css定位元素
《《《《《《《《同層級若遇到id用#號,遇到class用.
絕對路徑
相對路徑
多屬性定位,只支持 and的效果
模糊定位,稱為部分屬性值定位
#查詢子元素 # 子元素 a>b driver.find_element_by_css_selector('').send_keys('天天開心') # 后代元素 a b driver.find_element_by_css_selector('').send_keys('天天向上') # 第一個后代元素 :first-child driver.find_element_by_css_selector('div#u1 a:first-child').click() # 最后一個后代元素 :last-child driver.find_element_by_css_selector('div#u1 a:laat-child').click() 第n個子元素 :nth-child(n) 兄弟元素 同層級的多個元素用+ 新的定位語法 需導入by模塊 更適合用在框架里面 driver.find_element(By.ID,'KW').send_keys('xmx')