Python selenium with chrome webdriver - change user agent - python

I am running some code with selenium using python, and I figured out that I need to dynamically change the UserAgent after I already created the webdriver. Any advice if it is possible and how this could be done? Just to highlight - I want to change it on the fly, after almost each GET or POST request I send

I would go with creating a new driver and copy all the necessary attributes from the old driver except the user agent.

Related

How to change firefox profile preference after defining webdriver using Selenium and Python

How to change profile preference after defining driver?
profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(firefox_profile=profile)
After some code need to set useragent
profile.set_preference("general.useragent.override", ua)
How to set it without defining new driver?
As per the current implementation of Selenium once you configure the GeckoDriver with specific Capabilities and initialize the firefox session to open a Browsing Context, you cannot change the capabilities runtime. Even if you are able to retrieve the runtime capabilities still you won't be able to change them back.
So, in-order to change the Firefox User Preference you have to initiate a new WebDriver session.
Note:However, you can change the user-agent for Firefox on each run and you can find a relevant discussion in How to change user agent for Firefox webdriver in Python?
Reference
Here is #JimEvans clear and concise comment (as of Oct 24 '13 at 13:02) related to proxy settings capability:
When you set a proxy for any given driver, it is set only at the time WebDriver session is created; it cannot be changed at runtime. Even if you get the capabilities of the created session, you won't be able to change it. So the answer is, no, you must start a new session if you want to use different proxy settings.
Outro
You can find a couple of relevant detailed discussions in:
Python and selenium: how to change Firefox's profile multiple times
I believe it’s not possible but I found some workarounds explained in this article, not sure if those approaches are reliable though (or work at all): https://tarunlalwani.com/post/change-profile-settings-at-runtime-firefox-selenium/

Does the chromedriver used with Python Selenium store cookies

So my question is very simple. I would like know each time I run a selenium application using chromedriver, does it start with a blank slate? Or does it store cookies from last the last applicaion?
Selenium creates a temporary profile for each session, since profiles hold all the cookies, history, cache and settings for the browser the answer is no, it doesnt store cookies or any information from previous sessions.
That is if you don't explicitly override it with a custom profile dir when instantiating your driver

How to change proxy IP of selenium webdriver dynamically?

I‘m using Python & selenium writing a crawler program, once Chrome webdriver instance was created, is there a way to change the proxy IP dynamically? hope for your help, thanks in advance.
I don't believe you can change proxy without first destroying and creating a new browser instance. In my program where I implemented proxies I had a for loop create a new browser and then passed that browser as an argument to the functions I had created that needed it. Perhaps I am just ignorant of a way to dynamically do it, but as I recall the proxy information needs to be specified when the browser instance is created so again I would not think that what you are trying to do is possible...

How to access popup login form with Selenium in Python

I have been trying for a while to figure out how to enter username and password in the popup-window in this exercise:
http://pentesteracademylab.appspot.com//lab/webapp/digest
but I am entirely new to Selenium in Python. I found out how to click the button, so that the login form pops up:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://pentesteracademylab.appspot.com//lab/webapp/digest")
driver.find_element_by_css_selector('button').click()
but I cannot figure out how to access that window, let alone the fields in it. I have read about switch_to_frame and switch_to_window. For windows there is the window_handles showing you active windows to switch to, but this only returns a single element, which I believe is the main window, not the pop up. I also tried
alert = driver.switch_to_alert()
to no avail. The problem is that I do not know either which kind of object the popup is (frame,window,alert or something else), and I cannot find any names referring to it in the HTML code for the webpage.
Can anyone take me a step further?
Pass the authentication step by accessing the following URL:
http://username:password#pentesteracademylab.appspot.com/lab/webapp/digest/1
See also:
Handling browser level authentication using Selenium

How to switch Firefox profile with Selenium - without instantiating a new driver?

Trying to find a way to switch the active profile in a Selenium Firefox driver. This question about multiple Firefox profiles states that creating and using multiple profiles is possible. However, it would be better if there were some way to switch the active Firefox profile within the driver session - instead of starting a new one.
How can I switch the active Firefox profile with Selenium?
Thanks very much!
You can't. Selenium does not let you change Firefox profiles, and I'm pretty sure even Firefox doesn't.

Categories

Resources