Selenium

[selenium] Headless Chrome에서 user-agent 설정

thxxyj 2022. 7. 26. 11:59
728x90
from selenium import webdriver
from selenium.webdriver.common.by import By

# Headless Chrome(Chrome without Chrome)  background에서 Chrome을 실행하기
options = webdriver.ChromeOptions()
options.headless = True
options.add_argument('window-size=1920x1080')
options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/x.0.0.0 Safari/537.36')

browser = webdriver.Chrome(options=options)
browser.maximize_window()

url = 'https://www.whatismybrowser.com/detect/what-is-my-user-agent/'
browser.get(url)

detected_value = browser.find_element(By.ID, 'detected_value')
print(detected_value.text)
browser.quit()

########################################################
# 1. line 8 없이 실행하면 user-agent 값이 .../HeadlessChrome/... 으로 return
# Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/x.x.x.x Safari/537.36

# 2. user-agent 값을 argument로 지정할 경우
# Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/x.0.0.0 Safari/537.36
728x90