selenium設置瀏覽器為headless無頭模式(Chrome和Firefox)
新版本的selenium已經明確警告將不支持PhantomJS,建議使用headless的Chrome或FireFox。
兩者使用方式非常類似,基本步驟為:
- 下載驅動
- 創(chuàng)建選項,設定headless
- 創(chuàng)建WebDriver,指定驅動位置和選項
- 對URL發(fā)起請求,獲得結果,進行解析
Chrome
驅動的下載路徑為:https://chromedriver.storage.googleapis.com/index.html
接下來創(chuàng)建選項并設定headless:
options = webdriver.ChromeOptions() options.set_headless()
創(chuàng)建WebDriver,指定驅動位置和選項:
driver = webdriver.Chrome( 'D://chromedriver_win32//chromedriver', chrome_options=options)
發(fā)起請求,獲得結果并進行解析:
driver.get('http://www.sohu.com/') time.sleep(3) print(driver.page_source) driver.close()
Firefox
驅動的下載路徑為:https://github.com/mozilla/geckodriver
啟動的步驟與Chrome一致,只不過使用的選項對象和創(chuàng)建的WebDriver對象略有不同。直接上源代碼:
options = webdriver.FirefoxOptions() options.set_headless() driver = webdriver.Firefox( firefox_options=options, executable_path='D:/geckodriver-win64/geckodriver') driver.get('http://www.sohu.com/') time.sleep(3) print(driver.page_source) driver.close()
SELENIUM使用HEADLESS無頭模式實現(xiàn)無界面運行
先導包:
from selenium.webdriver.chrome.options import Options
加入如下配置:
chrome_options = Options() chrome_options.add_argument('--window-size=1920,1080') # 設置窗口界面大小 chrome_options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=chrome_options)
參考代碼:
from selenium import webdriver import time import multiprocessing from selenium.webdriver.chrome.options import Options class Zutuan(): def __init__(self): """打開瀏覽器""" self.chrome_options = Options() self.chrome_options.add_argument('--window-size=1920,1080') self.chrome_options.add_argument('--headless') self.driver = webdriver.Chrome(chrome_options=self.chrome_options) def open_zutuan(self, url): """傳入組團url""" self.driver.get(url) #self.driver.maximize_window() self.driver.refresh() #time.sleep(0.01) self.driver.implicitly_wait(30) # todo implicitly隱式等待,等待元素可見 def option_element(self, user, password): """xpath定位元素""" self.driver.find_element_by_xpath('//div[@class="login a"]/i').click() time.sleep(0.01) self.driver.find_element_by_xpath('//div[@class="a-title"]').click() self.driver.find_element_by_xpath('//input[@type="text" or @class="userName"]').send_keys(user) self.driver.find_element_by_xpath('//input[@type="password"]').send_keys(password) self.driver.find_element_by_xpath('//div[@class="button"]').click() time.sleep(1) self.driver.refresh() def select_commodity(self, content): """搜索組團商品""" # TODO self.content實例屬性傳給下面的方法使用,如果想把值給下面的方法用,添加實例屬性解決 self.content = content self.driver.find_element_by_xpath('//input[@type="text"]').send_keys(content) self.driver.find_element_by_xpath('//div[@class="search"]').click() self.driver.refresh() #return content def result(self): """判斷搜索商品成功后的提示信息,斷言頁面是否成功""" if self.content in self.driver.page_source: #print(self.content) print('商品搜索成功,測試通過') else: print('商品搜索錯誤,測試失敗') def closed(self): """關閉瀏覽器""" time.sleep(1) self.driver.quit() def run1(): # TODO 根據(jù)操作順序,調用方法執(zhí)行 zt = Zutuan() zt.open_zutuan('http://www.zutuan.cn/index.html#/') zt.option_element('1489088761@qq.com', 'mg123456') zt.select_commodity('香蕉') zt.result() zt.closed() class View_details(Zutuan): """把商品添加為明星單品,""" def check_commodity(self, number): """進入商品詳情頁,點擊添加明星單品""" self.driver.find_element_by_xpath('//a[@target="_blank"]/img').click() self.driver.switch_to.window(self.driver.window_handles[1]) self.driver.find_element_by_xpath('//div[@class="child start"]').click() self.driver.find_element_by_xpath('//div[@class="el-dialog__body"]//input[@type="text"]').send_keys(number) self.driver.find_element_by_xpath('//button[@type="button" and @class="el-button el-button--danger"]').click() time.sleep(1) def result(self): """重寫父類方法,判斷商品添加成功后的提示信息,斷言頁面是否成功""" if '添加成功' in self.driver.page_source: print('商品添加成功,測試通過') else: print('商品添加失敗,測試失敗') # 調用父類方法關閉 super().closed() def run2(): vd = View_details() vd.open_zutuan('http://www.zutuan.cn/index.html#/') vd.option_element('1489088761@qq.com', 'mg123456') vd.select_commodity('蘋果') vd.check_commodity(91628) vd.result() def main(): p1 = multiprocessing.Process(target=run1) p2 = multiprocessing.Process(target=run2) p1.start() p2.start() if __name__ == '__main__': main()
到此這篇關于selenium設置瀏覽器為headless無頭模式(Chrome和Firefox)的文章就介紹到這了,更多相關selenium 瀏覽器為headless無頭模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python對Excel不同的行分別復制不同的次數(shù)
這篇文章主要介紹了如何利用Python實現(xiàn)讀取Excel表格文件數(shù)據(jù),并將其中符合我們特定要求的那一行加以復制指定的次數(shù),感興趣的小伙伴可以學習一下2023-07-07Pandas:Series和DataFrame刪除指定軸上數(shù)據(jù)的方法
今天小編就為大家分享一篇Pandas:Series和DataFrame刪除指定軸上數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11python通過自定義isnumber函數(shù)判斷字符串是否為數(shù)字的方法
這篇文章主要介紹了python通過自定義isnumber函數(shù)判斷字符串是否為數(shù)字的方法,涉及Python操作字符串判斷的相關技巧,需要的朋友可以參考下2015-04-04