Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python selenium adjusts User Agent dynamically in an open browser

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "python selenium in the open browser how to dynamically adjust User Agent", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "python selenium in the open browser how to dynamically adjust User Agent" this article.

The core code is:

Browser.execute_cdp_cmd ('Network.setUserAgentOverride', {"userAgent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36", "platform": "Windows"})

Examples of use:

Import timefrom selenium import webdriverif _ _ name__ = ='_ main__': option = webdriver.ChromeOptions () option.add_argument ('lang=zh_CN.UTF-8') # set browser = webdriver.Chrome (chrome_options=option) browser.get ("https://www.baidu.com") print (browser.execute_script (" return navigator.userAgent ")) # print the current userAgent # dynamic adjustment useragent time. Sleep (3) browser.execute_cdp_cmd ('Network.setUserAgentOverride' {"userAgent": "Mozilla/5.0 (Windows NT 6.3) WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36 "," platform ":" Windows "}) print (browser.execute_script (" return navigator.userAgent ")) browser.get (" https://www.baidu.com") "

Print the results:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10-15-7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36

The first line is my chrome's original header, and the second line is the modified header

And you can see it in the browser's header:

The above is all the contents of the article "how python selenium adjusts User Agent dynamically in an open browser". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report