Reputation: 251
I am attempting to change my user agent and print the changed user agent to the terminal to check whether it has been successfully changed however I am having no luck.
I am using selenium wire and attempting to change it so i can login to a mobile version of the website. I cant put in the user agent i want due to security reasons however i have been at it for days and have no luck.
Please see my code below
driver = webdriver.Chrome('/Users/callum/Desktop/chromedriver')
def interceptor(request):
del request.headers['User-Agent'] request.headers['User-Agent'] = '####'
driver.get("https://www.google.com")
I also cannot print the user agent from selenium wire, i can only do it using this method.
agent = driver.execute_script("return navigator.userAgent")
print(agent)
Can someone please assist, it would be much appreciated :)
Upvotes: 1
Views: 5906
Reputation: 76
from seleniumwire import webdriver # Import from seleniumwire
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--user-agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166"')
browser = webdriver.Chrome(chrome_options=chrome_options)
user_agent = browser.execute_script("return navigator.userAgent;")
print(str(user_agent))
# Go to the Google home page
browser.get('https://www.google.com')
The same Chrome options that are mentioned in this question will also work here. For the printing of user-agent string see this question.
Upvotes: 4
Reputation: 929
Check out the mobile emulation capabilities of the Chrome driver:
https://chromedriver.chromium.org/mobile-emulation
Upvotes: 1