一、selenium庫安裝
pip install selenium
二、瀏覽器驅(qū)動安裝
谷歌瀏覽器驅(qū)動下載地址:https://chromedriver.storage.googleapis.com/index.html
根據(jù)你電腦的谷歌瀏覽器版本,下載相應(yīng)的就行。我下載的是110.0.5481.XX中的chromedriver_win32.zip
下載完成,解壓將里面的chromedriver.exe放到你python安裝路徑的scripts文件夾中。
三、簡單使用
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
能打開百度網(wǎng)頁說明安裝成功
四、各種使用方法
(一)定位法
input_box = browser.find_element_by_id("kw")
定位一個元素 | 定位多個元素 | 含義 |
---|---|---|
find_element_by_id | find_elements_by_id | 通過元素id定位 |
find_element_by_name | find_elements_by_name | 通過元素name定位 |
find_element_by_xpath | find_elements_by_xpath | 通過xpath表達式定位 |
find_element_by_link_text | find_elements_by_link_text | 通過完整超鏈接定位 |
find_element_by_partial_link_text | find_elements_by_partial_link_text | 通過部分鏈接定位 |
find_element_by_tag_name | find_elements_by_tag_name | 通過標(biāo)簽定位 |
find_element_by_class_name | find_elements_by_class_name | 通過類名進行定位 |
find_element_by_css_selector | find_elements_by_css_selector | 通過css選擇器進行定位 |
(二)獲取元素
1、獲取文本
from selenium.webdriver.common.by import By
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
search_button = browser.find_element(by=By.XPATH,value="/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/span[2]")
value1 = search_button.get_attribute("textContent") # 獲取文本方式1
value2 = search_button.get_attribute("innerText") # 獲取文本方式2
print(value1,value2) # =按圖片搜索
2、獲取value屬性值
browser.get('https://www.baidu.com')
search_button = browser.find_element_by_id("su")
value = search_button.get_attribute("value") # 獲取value屬性值
print(value) # =百度一下
(三)文本框輸入
browser.get('https://www.baidu.com')
search_button = browser.find_element(by=By.ID,value="kw")
search_button.send_keys("你好,世界!") # 文本框輸入
(四)按鈕點擊
1、鼠標(biāo)左擊
browser.get('https://www.baidu.com')
search_button = browser.find_element(by=By.ID,value="su")
search_button.click() # 點擊
2、鼠標(biāo)右擊
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
browser.get('https://www.baidu.com')
search_button = browser.find_element(by=By.ID,value="su")
ActionChains(browser).context_click(search_button).perform() # 鼠標(biāo)右擊
3、鼠標(biāo)雙擊
ActionChains(browser).double_click(search_button).perform() # 鼠標(biāo)雙擊
(五)瀏覽器操作
1、頁面刷新
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.refresh() # 當(dāng)前頁面刷新
2、修改窗口大小、全屏顯示
browser = webdriver.Chrome()
browser.set_window_size(800, 600) # 修改窗口大小
browser.maximize_window() # 全屏顯示
3、滑動進度條文章來源:http://www.zghlxwxcb.cn/news/detail-426832.html
browser = webdriver.Chrome()
browser.get('https://www.baidu.com/s?wd=你好世界')
browser.execute_script("window.scrollTo(0,300)") # 滑動進度條
4、關(guān)閉瀏覽器文章來源地址http://www.zghlxwxcb.cn/news/detail-426832.html
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com/s?wd=你好世界')
browser.close() # 關(guān)閉瀏覽器
到了這里,關(guān)于python之selenium庫安裝及用法(定位法、獲取文本、文本框輸入、鼠標(biāo)點擊、滑動滾動條)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!