Selenium尝试更改user-agent - 基于Python
今天翻查了下selenium更改User-Agent的文章,大多数都是以下这样的:
1 from selenium import webdriver 2 3 # 定义要将浏览器伪装成的ua 4 ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36', 5 # 创建浏览器选项对象 6 opts = webdriver.ChromeOptions() 7 opts.add_argument('user-agent=%s'%ua) # 添加user-agent 8 driver = webdriver.Chrome() # 浏览器对象 9 driver.get('https://www.baidu.com') # 请求baidu 10 print(driver.execute_script('return navigator.userAgent')) # Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36 11 print(driver.execute_script('return navigator.userAgent') == ua) #False
经测试,上面的代码,并没有改变浏览器的user-agent,它用的还是本身浏览器自带的。
有趣的测试:
1 import time 2 3 from selenium import webdriver 4 5 # 定义要将浏览器伪装成的ua 6 ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36', 7 driver = webdriver.Chrome() # 浏览器对象 8 driver.implicitly_wait(10) # 全局等待加载 9 driver.get('https://www.baidu.com') # 请求baidu 10 print("当前浏览器内置user-agent:", driver.execute_script('return navigator.userAgent')) # 返回当前浏览器的ua 11 12 driver.execute_script('window.open()') # 打开一个新窗口 13 driver.switch_to.window(driver.window_handles[-1]) # 切换到新打开的窗口 14 driver.get('https://www.163.com') # 在新窗口打开163 15 driver.execute_cdp_cmd('Network.setUserAgentOverride', 16 {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}) # 切换user-agent 17 print(driver.execute_script('return navigator.userAgent')) # 第一次更改user-agent 18 driver.execute_cdp_cmd('Network.setUserAgentOverride', 19 {'userAgent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0'}) # 火狐浏览器 20 print(driver.execute_script('return navigator.userAgent')) # 第二次更改user-agent 21 print('经更改后的user-agent为:', driver.execute_script('return navigator.userAgent')) 22 # 切换窗口,切换前休眠,以保证加载完成 23 time.sleep(5) 24 driver.switch_to.window(driver.window_handles[0]) # 切回第一个窗口,看user-agent是否更改 25 print('第一个浏览器窗口的user-agent为:', driver.execute_script('return navigator.userAget')) #None 26 driver.execute_cdp_cmd('Network.setUserAgentOverride', 27 {'userAgent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}) # 给第一个窗口添加ua 28 print(driver.execute_script('return navigator.userAgent'))
输出为:
1 当前浏览器内置user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36 2 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 3 Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0 4 经更改后的user-agent为: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0 5 第一个浏览器窗口的user-agent为: None 6 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36