最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

selenium設(shè)置瀏覽器為headless無頭模式(Chrome和Firefox)

 更新時(shí)間:2021年01月08日 10:02:34   作者:哦...  
這篇文章主要介紹了selenium設(shè)置瀏覽器為headless無頭模式(Chrome和Firefox),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

新版本的selenium已經(jīng)明確警告將不支持PhantomJS,建議使用headless的Chrome或FireFox。

兩者使用方式非常類似,基本步驟為:

  • 下載驅(qū)動
  • 創(chuàng)建選項(xiàng),設(shè)定headless
  • 創(chuàng)建WebDriver,指定驅(qū)動位置和選項(xiàng)
  • 對URL發(fā)起請求,獲得結(jié)果,進(jìn)行解析

Chrome

驅(qū)動的下載路徑為:https://chromedriver.storage.googleapis.com/index.html

接下來創(chuàng)建選項(xiàng)并設(shè)定headless:

options = webdriver.ChromeOptions()
options.set_headless()

創(chuàng)建WebDriver,指定驅(qū)動位置和選項(xiàng):

driver = webdriver.Chrome(
  'D://chromedriver_win32//chromedriver', chrome_options=options)

發(fā)起請求,獲得結(jié)果并進(jìn)行解析:

driver.get('http://www.sohu.com/')
time.sleep(3)
print(driver.page_source)
driver.close()

Firefox

驅(qū)動的下載路徑為:https://github.com/mozilla/geckodriver

啟動的步驟與Chrome一致,只不過使用的選項(xiàng)對象和創(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無頭模式實(shí)現(xiàn)無界面運(yùn)行

先導(dǎo)包:

from selenium.webdriver.chrome.options import Options

加入如下配置:

chrome_options = Options()

chrome_options.add_argument('--window-size=1920,1080')   # 設(shè)置窗口界面大小

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):
    """傳入組團(tuán)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):
    """搜索組團(tuán)商品"""
    # TODO self.content實(shí)例屬性傳給下面的方法使用,如果想把值給下面的方法用,添加實(shí)例屬性解決
    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('商品搜索錯(cuò)誤,測試失敗')

  def closed(self):
    """關(guān)閉瀏覽器"""
    time.sleep(1)
    self.driver.quit()


def run1():
  # TODO 根據(jù)操作順序,調(diào)用方法執(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):
    """進(jìn)入商品詳情頁,點(diǎn)擊添加明星單品"""
    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('商品添加失敗,測試失敗')
    # 調(diào)用父類方法關(guān)閉
    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()

到此這篇關(guān)于selenium設(shè)置瀏覽器為headless無頭模式(Chrome和Firefox)的文章就介紹到這了,更多相關(guān)selenium 瀏覽器為headless無頭模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 春節(jié)到了 教你使用python來搶票回家

    春節(jié)到了 教你使用python來搶票回家

    這篇文章主要介紹了春節(jié)到了 教你使用python來搶票回家,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 安裝pytorch時(shí)報(bào)sslerror錯(cuò)誤的解決方案

    安裝pytorch時(shí)報(bào)sslerror錯(cuò)誤的解決方案

    這篇文章主要介紹了安裝pytorch時(shí)報(bào)sslerror錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python 中[0]*2與0*2的區(qū)別說明

    python 中[0]*2與0*2的區(qū)別說明

    這篇文章主要介紹了python 中[0]*2與0*2的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • Python?PyCharm無法打開終端命令行最終解決方案(實(shí)測成功)

    Python?PyCharm無法打開終端命令行最終解決方案(實(shí)測成功)

    這篇文章主要介紹了在使用PyCharm?2024版本時(shí)遇到的無法打開終端的問題,文中提供了兩種解決方案,大家可以根據(jù)自己的需求選擇對應(yīng)的解決方法,需要的朋友可以參考下
    2024-12-12
  • python深度學(xué)習(xí)借助多標(biāo)簽分類器進(jìn)行對抗訓(xùn)練

    python深度學(xué)習(xí)借助多標(biāo)簽分類器進(jìn)行對抗訓(xùn)練

    這篇文章主要為大家介紹了python深度學(xué)習(xí)中如何借助多標(biāo)簽分類器進(jìn)行對抗訓(xùn)練,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-01-01
  • python3使用SMTP發(fā)送簡單文本郵件

    python3使用SMTP發(fā)送簡單文本郵件

    這篇文章主要為大家詳細(xì)介紹了python3使用SMTP發(fā)送簡單文本郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • python 異常處理總結(jié)

    python 異常處理總結(jié)

    這篇文章主要介紹了python 異常的相關(guān)資料,并整理了相關(guān)異常資料,需要的朋友可以參考下
    2016-10-10
  • 深入理解python虛擬機(jī)GIL詳解

    深入理解python虛擬機(jī)GIL詳解

    在目前的 CPython 當(dāng)中一直有一個(gè)臭名昭著的問題就是 GIL (Global Interpreter Lock ),就是全局解釋器鎖,他限制了 Python 在多核架構(gòu)當(dāng)中的性能,在本篇文章當(dāng)中我們將詳細(xì)分析一下 GIL 的利弊和 GIL 的 C 的源代碼
    2023-10-10
  • Python中django學(xué)習(xí)心得

    Python中django學(xué)習(xí)心得

    這篇文章主要介紹了Python中django Web應(yīng)用框架的學(xué)習(xí)做了總結(jié)并把心得體會寫了一下,大家一起參考下吧。
    2017-12-12
  • Python?中用多種方式實(shí)現(xiàn)單例模式

    Python?中用多種方式實(shí)現(xiàn)單例模式

    單例模式是一種常用的軟件設(shè)計(jì)模式,該模式的主要目的是確保某一個(gè)類只有一個(gè)實(shí)例存在,本文給大家分享Python?實(shí)現(xiàn)單例模式的五種寫法,感興趣的朋友一起看看吧
    2022-11-11

最新評論

五台县| 奉化市| 永康市| 安义县| 明光市| 蕉岭县| 奉新县| 曲周县| 祁连县| 会宁县| 宜川县| 尉犁县| 弋阳县| 资溪县| 搜索| 新安县| 河北省| 辽宁省| 宜州市| 工布江达县| 玉山县| 巴东县| 中超| 鲁山县| 长沙县| 珠海市| 龙山县| 东台市| 白银市| 台江县| 潢川县| 乌拉特后旗| 灌云县| 拉孜县| 鲜城| 娄烦县| 乐陵市| 东港市| 东兴市| 呼和浩特市| 新昌县|