I open a page, fill in fields, click submit. The submit does do a redirect. When I try $ie2.quit() it won't close the window. Any ideas?
$ie2 = New-Object -com InternetExplorer.Application
$ie2.visible=$true
$ie2.navigate("http://localhost/useradd.jsp")
start-sleep -seconds 3
$ie2.Document.getElementById("UserId").value = "test1"
$ie2.Document.getElementById("UserName").value = "test1 user"
$ie2.Document.getElementById("UserPassword").value = "12345$"
$submitButton = $ie2.document.getElementsByTagName("input")
Foreach($element in $submitButton )
{
#look for this field by value this is the field(look for screenshot below)
if($element.value -eq "Add"){
Write-Host $element.click()
}
$ie2.quit()
Try to add something like while ($ie2.Busy) { Start-Sleep -Seconds 1 } before the $ie2.quit() Statement to make sure, the COM Object is repsonsive.
Related
I am attempting to select a dropdown item via Powershell. Its using Javascript. So far I'm only achieved to login and get a bunch of methods by getting the Element. See below.
# Create an ie com object
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
# Wait for the page to load
while ($ie.Busy -eq $true){ Start-Sleep -Milliseconds 1000; }
# Login
Write-Host -ForegroundColor Green "Attempting to login.";
# Add login details
try
{
$ie.Document.IHTMLDocument3_getElementsByName("user") = $username
$ie.Document.IHTMLDocument3_getElementsByName("pass") = $password
$ie.Document.IHTMLDocument3_getElementsByName("submit")
Do{Start-Sleep -Milliseconds 100}While($ie.Busy -eq $True)
}
catch
{
$_.Exception.Message
}
#get dropdown elementarray
$ie.Document.IHTMLDocument3_getElementById('contentPlaceHolderId')
#get methods
$ie.Document.IHTMLDocument3_getElementById('contentPlaceHolderId') | gm
Does anybody know how to select a specific element in the placeholder? Methods are too many to fit in the post.
Thanks in advance.
For anybody else the answer is:
($ie.Document.IHTMLDocument3_getElementById('$contentPlaceHolderId') | Where-Object { $_.innerHTML -eq '$DropDownElementName' }).selected = $true
Clicking a button is:
$ie.Document.IHTMLDocument3_getElementById('$button').click();
I need to use PowerShell to hit close on this pop up window which appears when I open internet explorer. Hitting enter key also closes the pop up.
What I've tried
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$ie = new-object -com internetexplorer.application
$ie.visible = $true
$ie.navigate('http://website/')
while ($ie.busy) { Start-Sleep 3 }
[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")
[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}");
Start-Sleep 3
$link = $ie.Document.getElementsByTagName('Button') | where-object { $_.innerText -eq 'Simple Setup' }
$link.click()
Start-Sleep 2
$ie.quit()
Continuing from my comment.
Others have run into this dialog and others, and, as stated, used Selenium, AutoIT, et., to deal with that; while others have tried different means.
For Example:
# using the process handle of that dialog
$ws = New-Object -ComObject WScript.Shell
$ld = (gps iex* | where {$_.MainWindowTitle }).id
if($ld.Count -gt 1)
{
$ws.AppActivate($ld[1])
$ws.sendkeys("{ENTER}")
}
# Using the WASP module
# (note - though the code for this module is still available, the DLL is not. So, you have to compile that yourself.)
Import-Module WASP
while ($true) {
[System.Threading.Thread]::Sleep(200)
$confirmation = Select-Window iexplore
if ($confirmation -ne $null)
{
Select-ChildWindow -Window $confirmation |
Select-Control -title "OK" -recurse |
Send-Click
}
}
btw..
"Hitting enter key also closes the pop up"
... that is because modal dialogs always take focus until they are dismissed.
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
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
}
I have this script to launch IE, navigate to a page and search for some text:
$ie = new-object -com "InternetExplorer.Application"
$ie.Visible = $true
$ie.Navigate("http://www.google.com")
$doc = $ie.Document
if ($doc -eq $null)
{
Write-Host "The document is null."
return
}
$tb1 = $doc.getElementsByName("q") # a text box
$tb1.value = "search text";
$btn = $doc.getElementsByName("btnG")
$btn.click()
I save this as a ps1 file and run it from the command line... but the document object returned by $ie.Document is always null.
What am I doing wrong?
Also, when I run the script line by line in interpreter mode, the document is returned, but the next line $tb = $doc.getElementsByName("q") errors with this: Property 'Value' cannot be found on this object; make sure it exists and is settable.
How do I set the value of the text box, then?
You need to check if IE was done loading the page before $doc assignment. For example,
while ($ie.busy) {
#Sleep a bit
}
I tried the same code for entering the search text and button click but that did not work. So, ended up modiying your code to
$ie = new-object -com "InternetExplorer.Application"
$ie.Visible = $true
$ie.Navigate("http://www.google.com")
While ($ie.Busy) {
Sleep 2
}
$doc = $ie.Document
$btns = $doc.getElementsByTagName("input")
$SearchText = $btns | ? { $_.Name -eq "q" }
$SearchText.value = "search text"
$SearchButton = $btns | ? { $_.Name -eq "btnG" }
$SearchButton.click()
I believe there are two issues that I can see. First, Ravikahth's suggestion to add the ability to wait for the page to finish loading is important. If you do not wait for the page to load (i.e. $ie.busy -eq $false), then you will not get the full document.
Second, for whatever reason, Google decided to add multiple input fields with the name of "q." You can add a second condition to Ravikanth's query as stated below:
$SearchText = $btns | ? { $_.Name -eq "q" -and $_.Type -eq "text"}