change user-agent in python webdriver Selenium during navigation

Asked

Viewed 239 times

-1

need to perform the following procedure:

  • 1) browsing with user-agent mobile (I can do)
    • open the site www.site.com and login (I can do)
  • 2) Change user-agent to mobile(I cannot do)
    • access an option on www.site.com that only appears if you switch to mobile after logging in(from now on I can access it)

Well, my difficulty is to be able to change the user-agent to a mobile option already with the webdriver session open and in progress. For example, if you were to do this by hand, it would open Devtools with "Ctrl+Shift+I" and then "Ctrl+Shift+M", and that’s it, switch to mobile

But in python, for Selenium I only find instructions to start the webdriver session in mobile from the start of the execution. But for this site in specific I have to log in to Desktop mode, and only then change to mobile.

You don’t need to give me the code "chewed", if at least you have some hint of what I have to research to learn and then implement in the code already help.

Thank you

1 answer

0

Browse with user-agent mobile but don’t change the user-agent to mobile

Could use the Radom for each interaction change.

import

user_agent_list = ["Mozilla/5.0 (Linux; U; Android 4.3; de-de; GT-I9300 Build/JSS15J) Applewebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", "other agent"]

User Agents Android

user_agent = Random.Choice(user_agent_list)

headers = {'User-Agent': user_agent}

print(headers)

Unless you want to change the browser configuration for some mobile device, it is possible using the keyboard just add the lethal ones as well as the example of opening a new tab in the browser.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.