python selenium chrome有界面与无界面模式

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
 
 
# 无界面模式
def ChromeDriverNOBrowser():
   chrome_options = Options()
   chrome_options.add_argument('--headless')
   chrome_options.add_argument('--disable-gpu')
   driverChrome = webdriver.Chrome(executable_path="E:\chromedriver",chrome_options=chrome_options)
    return driverChrome
 
# 有界面的就简单了
def ChromeDriverBrowser():
    driverChrome = webdriver.Chrome(executable_path="E:\chromedriver")
    return driverChrome

  

用 headless 的 firefox

from selenium.webdriver.firefox.options import Options
ff_option = Options()
ff_option.add_argument('-headless')

  

firefox和chrome都可以设置无界面模式。

我一般在调试脚步的时候,可以使用有界面的模式,这样可以看到元素定位的步骤。有时候用chrome打开项目时,需要定位的元素是排在li[0]的位置,但是selenium调用chrome打开,该元素排序就出现变化,至今未明白;所以只能在调试的时候,用有界面的模式看清楚元素定位的情况;

调试好后,加入了HTMLTestRunner,就可以用无界面的来跑,只看报告就行了

原文地址:https://www.cnblogs.com/pythonClub/p/10505830.html