废话不多说,直接开干!
最近写项目,遇到常识,也是需要恶补知识点
PRO_DIR = os.path.dirname(os.path.abspath(__file__)) #获取当前执行文件路径
option.add_argument('start-maximized') #设置最大化运行窗口
option.add_argument('--disable-dev-shm-usage') #禁止共享内存
option.add_argument('--disable-extensions') #禁止扩展
option.add_argument('--disable-gpu') #禁用gpu
option.add_argument('--no-sandbox') #取消沙盘模式
option.add_argument('headless') #无界面运行窗口
option.add_argument('disable-infobars') #禁用浏览器正在被自动化程序控制提示
option.add_argument('--user-agent={}'.format(ua)) #请求头设置user-agent
看完整代码
import os
# selenium 无窗口运行模式
from selenium import webdriver
ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
timeStamp=1613735697#时间戳
PRO_DIR = os.path.dirname(os.path.abspath(__file__)) #获取当前执行文件路径
print(PRO_DIR)
def driver_sig():
option = webdriver.ChromeOptions()
option.add_argument('start-maximized') #设置最大化运行窗口
option.add_argument('--disable-dev-shm-usage') #禁止共享内存
option.add_argument('--disable-extensions') #禁止扩展
option.add_argument('--disable-gpu') #禁用gpu
option.add_argument('--no-sandbox') #取消沙盘模式
option.add_argument('headless') #无界面运行窗口
option.add_argument('disable-infobars') #禁用浏览器正在被自动化程序控制提示
option.add_argument('--user-agent={}'.format(ua)) #请求头设置user-agent
# driver = webdriver.Chrome(chrome_options=option, executable_path=executable_path)
driver = webdriver.Chrome(executable_path='D:\chrome\chromedriver.exe', chrome_options=option)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {D
get: () => undefined
})
"""})
driver.get('file:///' + PRO_DIR + '/search.html')
sig = driver.title
driver.quit()
return sig
res = driver_sig()
print(res)