I
I
IvanSstarr2019-07-31 12:04:42
PowerShell
IvanSstarr, 2019-07-31 12:04:42

How to change User Agent in Selenium WebDriver using ExecuteScript?

Good afternoon, the task is to change the User Agent (information about the browser, OS, etc., in the already running Selenium test on the fly, automatically, through the code, and not manually)
I write in PowerShell, but in Python the essence is the same:

add-type -path "C:\Program Files\WindowsPowerShell\Modules\Selenium\1.1\assemblies\WebDriver.dll"
$options = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 11.12; rv:68.0) Gecko/20100101 Firefox/67.0"
$options.addArguments("user-agent=$UserAgent");
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($options)
$Driver.Navigate().GoToUrl("https://2ip.ru/")

With this launch of the browser with the option, the User Agent changes to the desired one.
But how to change it after launching the browser? Perhaps somehow through ExecuteScript?
I know about various extensions for Google Chrome, like Random User-Agent, which can change it after launch, and even change it at a given interval, but then how can they be controlled through code, and not manually?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
azarij, 2019-07-31
@azarij

https://stackoverflow.com/questions/29916054/chang...
last answer, no?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question