Powershell, InternetExplorer.Application and Windows 11 (deleted IE) - windows

Working code in Windows 10 with IE
$mod_nums = #(19, 22)
$oIE = New-Object -ComObject 'InternetExplorer.Application'
$my_arr = New-Object System.Collections.Generic.List[System.Object]
foreach ($mod_num in $mod_nums) {
if ($null -eq $mod_num) {
break
}
$oIE.Navigate("https://wgmods.net/" + "$mod_num")
for ($i = 0; $i -le 5; $i++) {
do {
Start-Sleep -Milliseconds 100
} until ((-not $oIE.Busy) -and ($oIE.ReadyState -eq $READYSTATE_COMPLETE))
}
$oDocument = $oIE.Document
$oHtmlElement = $oDocument.GetElementsByClassName("ModDetails_hidden--2Rtru")[0]
$a = "$($oHtmlElement.href)"
$my_arr += $a
}
BUT in windows 11 IE deleted and $oIE.Navigate just open new window in Edge. Events, Methods, Properties dont work. Trying set IE mode in Edge - nothing changes.
Invoke-WebRequest is not suitable bec wgmods.net checks browser
Interested in any ideas about this. tnx!

IE is discontinued in Windows 11 and it's not expected that the old COM objects will continue to function. Per Internet Explorer mode and the DevTools:
If you have an existing application that uses the InternetExplorer object to automate Internet Explorer 11, but the Internet Explorer 11 desktop application isn't available, your application won't work. Internet Explorer 11 will be retired on June 15, 2022.
Despite the date however, Windows 11 does not have IE baked in so IE-specific automation won't work. The solution as mentioned in the same document above is to use the WebBrowser control to drive browser automation, which looks to include the IE mode use case:
Applications that require IE mode for the website (or app) content to function correctly should use the WebBrowser control. The WebBrowser control uses the Internet Explorer platform (MSHTML/Trident) to render web content, and will work even if the Internet Explorer 11 desktop application isn't available.
The good news is you have a few months until Windows 11 is released to get your automation updated, plus however long it takes for you to start using it in your environment.

Related

Adding a string to the Windows clipboard without adding it to the history in PowerShell?

I am using PowerShell to copy sensitive information to the Windows clipboard.
Since Windows 10 Vs. 1809 we have the enhanced clipboard with a history function. I don't want to have my sensitive information in the history.
The Set-Clipboard cmdlet doesn't have any helpful parameters. Even in C# I doesn't seem to have an easy way to do this.
It turns out you can access the Universal Windows Platform (UWP) APIs directly in PowerShell:
Function Set-ClipboardWithoutHistory([string]$Value)
{
[int]$RequestedOperationCopy = 1
$null = [Windows.ApplicationModel.DataTransfer.DataPackage,Windows.ApplicationModel.DataTransfer,ContentType=WindowsRuntime]
$null = [Windows.ApplicationModel.DataTransfer.ClipboardContentOptions,Windows.ApplicationModel.DataTransfer,ContentType=WindowsRuntime]
$null = [Windows.ApplicationModel.DataTransfer.Clipboard,Windows.ApplicationModel.DataTransfer,ContentType=WindowsRuntime]
$dataPackage = [Windows.ApplicationModel.DataTransfer.DataPackage]::new()
$cOptions = [Windows.ApplicationModel.DataTransfer.ClipboardContentOptions]::new()
$cOptions.IsAllowedInHistory = $false
$cOptions.IsRoamable = $false
$dataPackage.RequestedOperation = $RequestedOperationCopy
$dataPackage.SetText($Value)
[Windows.ApplicationModel.DataTransfer.Clipboard]::SetContentWithOptions($dataPackage, $cOptions) | Out-Null
}
As of December 2019, PowerShell is still not able to naively prevent adding a new clipboard item to the clipboard history.
The APIs to do this are available for the Universal Windows Platform (UWP).
During 2019 Microsoft made it much easier to access certain APIs in UWP from Windows Desktop Apps, and as it turns out from PowerShell.
So I wrote a small .Net assembly that I can use in PowerShell to prevent the usage of the history feature when copying text to the Windows clipboard.
The trick is to use:
Microsoft.Windows.SDK.Contracts
to access the UWP APIs, then use:
using uwp = Windows.ApplicationModel.DataTransfer;
uwp.DataPackage dataPackage = new uwp.DataPackage { RequestedOperation = uwp.DataPackageOperation.Copy };
dataPackage.SetText("text to copy");
uwp.Clipboard.SetContentWithOptions(dataPackage, new uwp.ClipboardContentOptions() { IsAllowedInHistory = false, IsRoamable = false });
uwp.Clipboard.Flush();

Wy doesn't my VBScript do the same thing on Windows 10 as it does on Windows 7?

I've created a batch file that runs some VBScript which works fine in Windows 7 but not in Windows 10. Why doesn't it not work on both?
The batch file does 6 things which are listed below. On Windows 7 all 6 things happen. On Windows 10 all but item 5 take place and the window title is not changed.
opens Internet Explorer
brings the window to the foreground
navigates to a webpage
waits 1.5 seconds to allow the webpage to load
changes the window title from "MultiSmart" to "Community MultiSmart"
exits
Here is the the batch file.
<!-- :
#echo off
cscript //nologo "%~f0?.wsf" %*
exit /b
-->
<job>
<script language="VBScript">
Set ie = WScript.CreateObject("InternetExplorer.Application")
'ie properties
ie.ToolBar = 0
ie.StatusBar = 0
ie.Width = 816
ie.Height = 519
ie.Visible = 1
ie.Resizable = 0
'bring window to foreground
CreateObject("WScript.Shell").AppActivate "Internet Explorer"
'navigate to Stony Mountain Lift Station's Multismart
ie.Navigate("http://192.168.0.11/")
'wait for page to load into browser
Wscript.Sleep 1500
'change window title
ie.document.title="Community MultiSmart"
</script>
</job>
I would double check that the webpage is fully loaded.
Change this line:
Wscript.Sleep 1500
to
While IE.ReadyState < 4
Wscript.Sleep 250
Wend
It's unclear as to whether npocmaka's comment of:
"...Add metadata to force old browser behavior on windows 10."
worked for you or not. If not, then consider this alternative:
Check your security settings in IE.
Warning: This answer provides suggestions that may lower your system's security settings. It is advised that you fully understand the risks involved before proceeding and be proactive with using alternative protective measures. (Or, just stop automating in IE ☺)
Go to IE > Internet Options > Security Tab
(Optional) Add the website to your 'Trusted Sites' zone if you trust the site (which will make the next step easier on you)
Whichever zone you decided to keep site in (either 'Internet' or 'Trusted Sites'), uncheck the box: Enable Protected Mode (requires restart). Warning: This is obviously going to reduce the security of IE.
Click Apply then OK
Close out of ALL IE BROWSERS to ensure that the change takes effect
Double check Task Manager (Ctrl-Shift-Esc) to ensure no hidden iexplore.exe processes exist in the Processes tab.
Test the end result
If still doesn't work, ensure you reverse any settings.
Scripting can obviously be dangerous to any system, which is why Protected mode may severely limit what you can do with automation. I believe that IE didn't enable protected mode in IE by default until Windows 8(.1)?, but I don't have a source for my suspicions; if this is the case, that would be why it works on Windows 7 and not Windows 10.
You can still do simple things with protected mode enabled, but it's severely limited. For example, you can still navigate to web pages, but many of IE's other properties and methods are disabled.
I'm re-posting npocmaka's comment which answered my question as an answer.
because it depends on the internet explorer version
The versions were different. What worked on IE 11.0.966.18920 did not on IE 11.461.16299.0.

Can't get Selenium tests to run in Internet Explorer

We have been successfully using Selenium tests on Chrome and Firefox, we now want to start testing on a Windows 10 Virtual Machine with Internet Explorer too. We are so far now that the tests start, and Internet Explorer opens and goes to the page we want to test. But any interactions with the page fail the test, it doesn't find any of the elements. The error is usually 'element cannot be found by xpath'. The same tests run fine in Chrome and Firefox on the same machine.
Here are few checklist to execute through Internet Explorer:
Ensure that you are using the latest Selenium 3.x jars along with IE 11.
Next you need to download the "IEDriverServer" (32 bit) from here.
Provide the absolute path of the "IEDriverServer" while setting the system property.
Here is the code for IE 11 to open gmail and login with valid credentials:
(replace your_id with a valid userid & replace your_password with a valid password)
//Internet Explorer 11
System.setProperty("webdriver.ie.driver", "C:\\SeleniumUtilities\\BrowserDrivers\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("http:\\\\gmail.com");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.id("Email")).sendKeys("your_id");
driver.findElement(By.id("next")).click();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
driver.findElement(By.id("Passwd")).sendKeys("your_password");
driver.findElement(By.id("signIn")).click();
driver.quit();
Let me know if it works for you.
I got it to work. The problem was that I needed to turn off protected mode for all security zones in internet explorer -> internet settings -> security.

Watin - how I can do the testing on different versions of Internet Explorer?

I need to do tests on different versions of Internet Explorer browser, but not WATIN.CORE.IE a method that would alter the version of browser used. I hope you understand my problem.
You will need multiple virtual machines each installed with a different version of ie because you cannot install ie versions side by side (There are a few hacks but you never get a true representation).
You can query the registry to get the IE version.
To output the full version number to the NUnit console do the following.
var ieKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Internet Explorer");
if (ieKey == null)
{
Console.WriteLine("IE key not found");
}
else
{
Console.WriteLine("Version:" + (string)ieKey.GetValue("Version"));
}
The above was verified using: Windows 7, IE8 and WatiN 2.0
Thanks be to Jeroen as the registry call is copied verbatim out of IE.cs

Having problems running WatiN on Windows 7 with IE 8

When I run any WatiN test on Windows 7 with IE8(note that all tests pass on Vista with IE8), the browser displays the first page but does not go any further. The following exception is displayed after a few seconds:
WatiN.Core.Exceptions.TimeoutException: Timeout while Internet Explorer state not complete
at WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.ThrowTimeOutException(Exception lastException, String message)
at WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.HandleTimeOut()
at WatiN.Core.UtilityClasses.TryFuncUntilTimeOut.Try(DoFunc1 func)
at WatiN.Core.WaitForCompleteBase.WaitUntil(DoFunc1 waitWhile, BuildTimeOutExceptionMessage exceptionMessage)
at WatiN.Core.Native.InternetExplorer.WaitForComplete.WaitWhileIEReadyStateNotComplete(IWebBrowser2 ie)
at WatiN.Core.Native.InternetExplorer.IEWaitForComplete.DoWait()
at WatiN.Core.DomContainer.WaitForComplete(IWait waitForComplete)
at WatiN.Core.IE.WaitForComplete(Int32 waitForCompleteTimeOut)
at WatiN.Core.DomContainer.WaitForComplete()
at WatiN.Core.Browser.GoTo(Uri url)
at WatiN.Core.IE.FinishInitialization(Uri uri)
at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, IDialogHandler logonDialogHandler, Boolean createInNewProcess)
at WatiN.Core.IE..ctor(String url)
at CCS.iPS.ST.Tests.UIWithDBVerification.Tests.DCC_Offered_Completed_ThreeDS_And_Authorisation_Completed() in Tests.cs: line 18
Make sure you are running as an Administrator. Seems to be an issue where Watin can't access the DOM in IE unless the application is running with System Administrator privileges.
I know this is an ancient thread, but I've found found a workaround for WatiN under Windows 7 that doesn't require you to run as an administrator (which isn't allowed in my company :S) If you disable protected mode in Internet Explorer it should run fine: -
1 - Open internet explorer.
2 - Click on Tools menu and select Internet Options.
3 - Select Security Tab in the Internet options windows.
4 - Select Internet from the zone settings.
5 - Uncheck Enable Protected Mode option to disable the protection from Security for this zone.
6 - Hit Apply and Ok

Resources