I am creating a script with python + selenium + firefox. While loading firefox I am including the following addon - https://github.com/muzuiget/user_agent_overrider . It overrides user-agent with just one click.
I need to simulate that from my code. So what i need is the following:
1. I open a page with default user-agent.
2. Script should change the user-agent while Firefox is still running (using the addon from above) and visits the same page.
I did look at the source code of the addon, but I just can't trigger the right function :)
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
ff_profile = FirefoxProfile()
ff_profile.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36")
driver = webdriver.Firefox(firefox_profle = ff_profile)
driver.get('http://www.whatismybrowser.org')
There is a typo in the last answer.. i did a stupid copy/paste and it took me 20 minutes to realise it -_-
driver = webdriver.Firefox(firefox_profle = ff_profile)
should be
driver = webdriver.Firefox(firefox_profile = ff_profile)
Related
i am creating a desktop application with Webview2 as browser. I have to change the user agent to get data from a website. My control name is Webview21 for the browser control. I got the error message System.NotImplementedException: 'Unable to cast to Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Settings2.
This may happen if you are using an interface not supported by the version of the WebView2 Runtime . But i have the latest evergreen version.
What is the solution?
Public Async Sub InitializeBrowser()
Dim strip As String = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62 seakayak-navigator"
Dim opts As New CoreWebView2EnvironmentOptions With {.AdditionalBrowserArguments = "--disable-web-security --allow-file-access-from-files --allow-file-access "}
Dim userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\SeakayakNavigator"
Dim env = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder)
Await WebView21.EnsureCoreWebView2Async(env)
Application.DoEvents()
WebView21.Width = Fmenu.Width
WebView21.Height = Fmenu.Height
WebView21.Top = 0
WebView21.Left = 0
WebView21.BringToFront()
WebView21.CoreWebView2.Settings.UserAgent = strip
' Add any initialization after the InitializeComponent() call.
AddHandler WebView21.NavigationCompleted, AddressOf webview21_navigationCompleted
WebView21.CoreWebView2.AddHostObjectToScript("Bridge", New Bridge())
End Sub
CoreWebView2.Settings.UserAgentis not supported in the current stable release (version 1.0.818.41).
If you want to use CoreWebView2.Settings.UserAgent you must install the pre-release version, currently version 1.0.865-prerelease.
You can follow the directions here: To install a pre-release version
That also means that you must use Microsoft Edge CANARY build
which you can get here: Microsoft Edge Insider Channels
Unfortunately, you might have to uninstall the 'WebView2 runtime' (which I think is a bug in the WebView2 package).
Update:
Now the latest stable version supports setting the
useragent.
I created a Web Performance and Load Test Project in Visual Studio 2015. I created a Load test and in the wizard it gives me a list of available browsers to simulate. The browsers it lists are extremely old (Chrome 2, Netscape).
Anyway to update the list?
The list of browsers is taken from the files in a Visual Studio directory. For the 2013 version the directory is as follows. Similarly named directories are used for other Visual Studio versions.
c:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Templates\LoadTest\Browsers
The actual *.browser files contain XML and can be edited with Notepad etc. If you know the characteristics of the browser you want to simulate then it should be simple to copy and modify an existing file.
I have not seen any web page that specifies the details of the *.browser files. Part of their content is the "user agent string" and web searching should readily find the string for many new browsers.
Be aware that standalone web tests are run differently than those run within a load test. Standalone web tests are run as if by a default browser. Have not found any way to specify that browser. Many characteristics of the real browser may be irrelevant but the user agent string may be important. I wrote the following plugin when testing a web site for it behavior when accessed by a mobile phone or a tablet.
[System.ComponentModel.Description(
"Set the user agent to a fixed value if called from a web test. For a load "
+ "test leave it alone so the value from the browser mix is used.")]
public class SetUserAgent : WebTestPlugin
{
public override void PreRequest(object sender, PreRequestEventArgs e)
{
if (e.WebTest.Context.ContainsKey("$LoadTestUserContext"))
{
// Leave the user agent alone, it will be set by the load test's browser mix.
}
else
{
const string UserAgent = "User-Agent";
if (e.Request.Headers.Contains(UserAgent))
{
e.Request.Headers.Remove(UserAgent);
}
// Samsung Galaxy Tab 3 7inch.
// e.Request.Headers.Add(UserAgent, "Mozilla/5.0 (Linux; U; Android 4.2.2; en-gb; SM-T110 Build/JDQ39) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30");
// Sony Xeperia S, Ion. 360x640 pixels.
e.Request.Headers.Add(UserAgent, "Mozilla/5.0 (Linux; U; Android 4.0; en-us; LT28at Build/6.1.C.1.111) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
}
}
}
It should be easy to set user agent strings for other devices.
Is there a way to send keystrokes to a specific window using PowerShell ?
Right now I'm opening an IE window.
Then, by calling the link, a download window is opening.
I'm sending an ALT+TAB to switch in the download window, and LEFT+ENTER to confirm the download.
Is there a way to send this LEFT+ENTER to a specific window without having focus on it?
My code looks like that:
$ie=new-object -com internetexplorer.application
$ie.navigate2($url)
$ie.visible=$true
while($ie.busy) {Start-Sleep 1}
start-sleep -seconds 1
[System.Windows.Forms.SendKeys]::SendWait("%{TAB}")
start-sleep -seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{LEFT}")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
I want to send the keystrokes to a specific window to make sure the download window gets the keystrokes even if the focus isn't on it.
I know sending keystrokes isn't the best way to download the file, but I've also tried this:
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.Networkcredential($user, $password)
$webclient.DownloadFile($urlWeb,$path)
I always get an 403 Error and haven't found a solution yet to solve this problem...
I'm not really a fan of SendKeys cause it almost always fail. If I use the provided link, I'm able to download the file with this Powershell code:
$userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
$target = "http://download.thinkbroadband.com/5MB.zip"
$folder = "C:\TEMP\5MB.zip"
$web = New-Object System.Net.WebClient
$web.Headers.Add("user-agent", $userAgent)
$web.DownloadFile($target, $folder)
You might also need to add network credentials.
Sometimes CRM 2013 decides to give me the following error when opening an Order (and presents me with an empty page, but with the top navigation bar intact):
<CrmScriptErrorReport>
<ReportVersion>1.0</ReportVersion>
<ScriptErrorDetails>
<Message>Unable to get property 'ClientVariables' of undefined or null reference</Message>
<Line>2</Line>
<URL>/_common/JsProvider.ashx?ids=1959345455-451858892-1713948411-1279630416-1250882489-367493978-19173614-574022791-696891199-2007586035-1552783829&ver=-976933632</URL>
<PageURL>/main.aspx?etc=1088&extraqs=%3f_gridType%3d1088%26etc%3d1088%26id%3d%257b76EB6220-FB46-E311-93F3-00155D5752A9%257d%26rskey%3d674031362&pagemode=iframe&pagetype=entityrecord&rskey=674031362 </PageURL>
<Function>anonymous(){returnthis.get_$L_0().ClientVariables}</Function>
<CallStack>
<Function>anonymous(){returnthis.get_$L_0().ClientVariables}</Function>
</CallStack>
</ScriptErrorDetails>
<ClientInformation>
<BrowserUserAgent>Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C)</BrowserUserAgent>
<BrowserLanguage>en-US</BrowserLanguage>
<SystemLanguage>en-US</SystemLanguage>
<UserLanguage>en-US</UserLanguage>
<ScreenResolution>1920x1080</ScreenResolution>
<ClientName>Web</ClientName>
<ClientTime>2013-11-11T17:32:05</ClientTime>
</ClientInformation>
<ServerInformation>
<OrgLanguage>1033</OrgLanguage>
<OrgCulture>1043</OrgCulture>
<UserLanguage>1033</UserLanguage>
<UserCulture>1043</UserCulture>
<OrgID>{54131DE3-6D18-44FD-B4D0-0A9E87DA3E9D}</OrgID>
<UserID>{E369A31E-3446-E311-93F2-00155D5752A9}</UserID>
<CRMVersion>6.0.0.809</CRMVersion>
</ServerInformation>
</CrmScriptErrorReport>
This will keep on happening until I fiddle around with the cache or open another organisation. I'm still not sure what exactly resolves it. I do have custom code on the Order form but I often go days without getting the error. Any idea how I can try to locate the cause of this error and whether it has something to do with my custom code or whether its a bug in CRM 2013?
Thanks.
I've faced same issue with quote entity, when I ask my CRM users to clean temporary files for the browsers they never reported the issue.
In my case MSD 2013 On-premise
I'm developing on a Win7 machine with IE8 browser (same thing occurs also on IE9).
I've create an application with an IWebBrowser2 object embedded within a native window.
Within that browser object i execute a javascipt code:
var txt = "Browser Name: " + navigator.appName + "\n";
txt += "Browser Version: " + navigator.appVersion + "\n";
alert(txt);
and i receive the following alert:
That confirmed my earlier worries - even when having IE8\IE9 installed on my machine - the IWebBrowser2 object behaves as if it was IE7 (including all of the annoying HTML\CSS behaviors).
Does anyone know why this is? or better - if there is any possibility to embed a browser object as IE8\IE9 (for distribution - so it should be legal)
Thanks,
IE7 is the default rendering mode for embedded IE controls (for compatibility reasons). Use the FEATURE_BROWSER_EMULATION registry key to change that.
Read Plip's answer over here or IEBlog to get an idea how to do this.