We are trying to use Powershell to automate some testing on our site.
We have hit a problem when we issue the click event on an anchor which opens a new tab.
We need to get a handle on the new tab and then continue processing by clicking the print button on the new tab.
How could this be done using Powershell?
set-executionpolicy remotesigned
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("xxxxx.com")
$ie.visible = $true
$doc = $ie.document
While ($ie.Busy) {Sleep 10}
$doc -eq $null
$tb1 = $doc.getElementByID("username")
$tb2 = $doc.getElementByID("password")
$tb1.value = "xxxxxxxxxxx"
$tb2.value = "xxxxxxxxxxx"
$btn = $ie.document.getelementsbytagname("input")|where{$_.name -eq "Submit"}
$btn.click()
$ie.navigate("xxxxx.com/admin/reports.html#")
$link = $ie.Document.getElementsByTagName('A') | where-object {$_.innerText -eq 'All Photos'}
$link.click()
<<<<<<<<<<< it's here where a new tab is open and we need to get a handle on the new page to be able to click the print button >>>>>>>>>>>
$popupHandle = $ie.openwindow.winrefs[$targetName]
$btn = $popupHandle.document.getelementsbytagname("input")|where{$_.name -eq "print"}
$btn.click()
We are new to Powershell so any help would be appreciated.
Thanks
Richard
Try opening your new tab using $ie.navigate2 and 0x0800 to put it in a new foreground tab. That will automatically focus you on the new tab allowing you to grab the print button.
e.g.
# Setting page to new foreground tab here
$newTabPage = $ie.navigate2("www.xxxx.com",0x0800)
Similarly, you can use 0x1000 to open the page in a new background tab, which will open the new tab but not automatically bring it to focus.
# Setting page to new background tab here
$newTabPage = $ie.navigate2("www.xxxx.com",0x1000)
Related
I have a script for Internet Explorer Login which works fine. But I want to go further because after the first login, Internet Explorer opens a new tab, with another Login form. So $IE.visible = $False does not work for the second tab, which opens automatically, and also I dont know how to give focus to the second tab to input other credentials and also put it on invisible.
$variablePass = $variablePass.Text
$username = "testUser"
$username2 = "testUser2"
$password = "testPass" + $variablePass
$password2 = "testPass2"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $false
$ie.Height = 720
$ie.Width = 1280
$ie.navigate("https://mylink.com")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
#This one work fine
$ie.document.getElementById("username").value = "$username"
$ie.document.getElementById("password").value = "$password"
$ie.document.getElementById("btnSubmit").click()
# Here comes the problem, after IE reach mylink.com and do the login script, the a new tab pop up with another login form
$ie.document.getElementById("username").value = "$username2"
$ie.document.getElementById("password").value = "$password2"
$ie.document.getElementById("loginBtn").click()
So, how do I do the second Login form to work. Preferably in invisible mode, I mean $IE.visible = $False
Are you able to change the configuration of IE to prevent this from happening?
https://helpdeskgeek.com/how-to/force-ie-to-open-link-in-new-tab/
You could manually configure IE, or use PowerShell to edit the registry to force that specific configuration...
I have a powershell script I'm running on startup to open a website and log in. It works fine whenever I run the script manually, or whenever I sign out of my profile in Windows and sign back in. However, when I restart the computer, the script opens the website, but then fails to edit the DOM to change the username and password text fields and click the submit button. It seems to attempt to change the values because the cursor stops blinking, but nothing happens.
Here is the code.
$IEProcess = [System.Diagnostics.Process]::Start("iexplore", "-k https://www.website.com")
Sleep -Seconds 1
$IE = $(New-Object -ComObject "Shell.Application").Windows() | ? {$_.HWND -eq $IEProcess.MainWindowHandle}
while ($IE.ReadyState -ne 4)
{
Start-Sleep -Milliseconds 100
}
$IE.Document.getElementById(“userNameInput”).value = $Username
$IE.Document.getElementByID(“passwordInput”).value= $Password
$IE.Document.getElementById(“submitButton”).Click()
Using this code, I was able to get it to work. It may have to do with how you are configuring it to run on startup.
$Url = "https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%
2f%2fstackoverflow.com%2f"
$Username="name#email.com"
$Password="password"
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true;
$IE.navigate($url);
while ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 2000;
}
$IE.Document.getElementById("email").value = $Username
$IE.Document.getElementByID("password").value=$Password
$IE.Document.getElementById("submit-button").Click()
I did the following:
Open gpedit.msc
User config > Windows Settings > Scripts (Logon/Logoff)
"PowerShell Scripts" tab
Add script
I have a problem to click the login button on the following page:
https://community.theme.co/wp/wp-login.php
I have tried to click the button with code below:
$submitButton = $ie.document.getElementById("wp-submit").Click();
foreach ($element in $submitButton)
{
if ($element.Name -eq "wp-submit") {
$element.Click()
}
}
I think checking ie object's busy property might fix it.
$url= "https://community.theme.co/wp/wp-login.php"
$ie = new-object -ComObject "internetexplorer.application"
$ie.Navigate2($url)
$ie.Visible =$true
while($ie.Busy){
start-sleep 10
}
$submitButton = $ie.document.getElementById("wp-submit").click();
could not confirm as i don't have any valid credential but i think the click was happening
Im using windows Windows.Forms.RichTextBox to redirect my powershell script output "$var".
Detect.Urls is already enabled and working, but unable to open them by clicking.
Can any one help me with the code for link click event handler in powershell script........
$outputBox = New-Object System.Windows.Forms.RichTextBox
$outputBox.Location = New-Object System.Drawing.Size(10,150)
$outputBox.Size = New-Object System.Drawing.Size(700,300)
$outputBox.MultiLine = $True
$outputBox.SelectionIndent = 8
$outputBox.SelectionHangingIndent = 3
$outputBox.SelectionRightIndent = 12
$outputBox.ScrollBars = "ForcedBoth"
$Form.Controls.Add($outputBox)
$outputBox.Text = $var
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
You have to handle click events by yourself
$outputBox.add_LinkClicked({
Start-Process -FilePath $_.LinkText
})
Will open link in the default browser when clicked.
This is how I do it with Powershell Studio...right click the control and add a new event...add the linkclicked event and then go to the script and add the following.
depending on what you want to be opened you may need to used something other than explorer, but the $_.linktext should have the link you want from the url. note if there are spaces you will have to something to replace them as the url will be broke at the first space it encounters.
$Linkclicked = $_.LinkText
explorer.exe $Linkclicked
I'm trying to make a powershell script that would open up a new IE window from javascript link and I would like to read a text from that new window. So far, the script would only read the content from original IE window and I'm not sure how to get the script to read content off from new IE page.
$ie = new-object -com InternetExplorer.Application
$ie.navigate("https://www.testsite.com/")
do {sleep 1} until (-not ($ie.Busy))
$ie.visible = $true
$doc = $ie.document
if ($ie.document.documentelement.innerText.Contains('Welcome') -eq "True") {
"Site is RUNNING"
} else {
"Site is STOPPED"
}
$link = #($ie.Document.getElementsByTagName('A')) | Where-Object {$_.innerText -eq 'Click here for new site'}
$link.click()
if ($ie.document.documentelement.innerText.Contains('New Page') -eq "True") {
"Site is RUNNING"
} else {
"Site is STOPPED"
}
New window pops up after $link.click(), and I don't know how to read anything from the new window. I could read the text from original www.test.com/ page but not from the new page.
Thank you.
PT
You can use the following code to iterate over all IE windows/tabs and select the one with the title you're looking for:
$title = '...'
$ie2 = (New-Object -COM 'Shell.Application').Windows() | ? {
$_.Name -eq 'Windows Internet Explorer' -and $_.LocationName -eq $title
}