I am not sure if I mentioned the correct subject. But I will try to explain.
I am trying to do a search in a website, and then based on the result, I am trying to fetch some values.
The website is https://dotdb.com/.
When I manually go to the website, and search something (lets say 'facebook.com') and hit 'Search', I get the result which looks like this:
If you search manually, then you will notice that initially there is a loading icon on the three place (highlighted in yellow in above image),and then the values get populated.
Now, when I try to perform the same operation using Powershell IE Automation, I always see the loading icon and the value never gets set there. Therefore, I am not able to fetch the value. Here is the screenshot of the output from my powershell script:
Finally here is my powershell script:
$ie = New-Object -com internetexplorer.application
$ie.visible = $true
$ie.navigate("https://dotdb.com")
while ($ie.Busy -eq $true) {
Start-Sleep -Seconds 1;
} #wait for browser idle
($ie.document.getElementsByName("keyword") | select -first 1).value = "facebook.com"
($ie.document.getElementsByTagName('button')[2]).click()
while($ie.Busy -eq $true) {
Start-Sleep -Seconds 1;
} #wait for browser idle
Please guide me to fetch those values.
Thanks in advance!
Related
I want to automate a task at my work where i need to fill a form in one of our intranet sites. Basically i need to call the site, fill a specific textbox and click the save button. I am trying first to test the code on Google website but it is not working, although i am copying most of it from various sources.
The code mostly works fine. I am able to open Internet explorer, navigate to google, fill the search textbox, but i am stuck when clicking the button.
$Url
$Textbox
$Url = “www.google.com”
$Textbox=”test”
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true
$IE.navigate($url)
while ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 2000
}
$IE.Document.getElementsByname(“q”)[0].value = $Textbox
$IE.Document.getElementsByname(“btnk”)[0].Click()
while ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 2000
}
The error message i keep getting is this:
You cannot call a method on a null-valued expression. At line:17
char:1
+ $IE.Document.getElementsByname(“btnk”)[0].Click()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
enter image description here
getElementsByname name parameter is case sensitive.
The name of the Search element is btnK and not btnk.
Change to:
$IE.Document.getElementsByname("btnK")[0].Click()
I have reproduced the problem on my machine, it seems that this behavior is related to the IE browser protected mode. Before turning on the protected mode, it will show this behavior.
To solve this issue, please turn on the Protected Mode (in my machine, I enabled the protected mode in different zones. The screenshot like this).
Besides, you could also run the PowerShell as admin.
The PowerShell script as below:
$Url
$Textbox
$Url = "www.google.com"
$Textbox="test"
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true
$IE.navigate($url)
while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000 }
Start-Sleep -Seconds 1
$IE.Document.getElementsByName("q")[0].value = $Textbox
Start-Sleep -Seconds 1
$IE.Document.getElementsByName("btnK")[0].Click()
while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000 }
I've always used:
$Button_Name.RaiseEvent((New-Object -TypeName System.Windows.RoutedEventArgs $([System.Windows.Controls.Button]::ClickEvent)))
Because it works for me and I'm too lazy to look for anything else.
I am trying to get the HTML code from an Intranet webpage and monitor if certain texts or titles exist. This powershell code will be used by my monitoring program to trigger alerts when the webpage is down so that I cannot see that certain texts or titles.
For now, I'm just using Write-Host to see if my piece of code works. I can now extract the HTML source to $output, and I am sure 'Create!' can be found inside. However, I'm not getting a 'YES'.
May I know if $output can be checked by using -contains?
Thank you very much for your help!
$targetUrl = 'https://myUrl/'
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate($targetUrl)
while($ie.Busy) {
Start-Sleep -m 2000
}
$output = $ie.Document.body.innerHTML
if($output -contains '*Create!*')
{Write-Host 'YES'}
else
{Write-Host 'NO'}
The operator -contains is used to search collections. The IE's innerHTML is just a string:
$output = $ie.Document.body.innerHTML
$output.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Use pattern matching operators like, well, -like and -match.
By the way, if IE is not mandatory, try Invoke-WebRequest cmdlet.
I would like to automate a task from my work using MS Powershell. Please, see my code below that log in the website. This code is working fine.
$username = "usern"
$password = "pass"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("http://www.exemple.com")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.IHTMLDocument3_getElementByID("textfield").value = $username
$ie.document.IHTMLDocument3_getElementByID("textfield2").value = $password
$ie.document.IHTMLDocument3_getElementByID("btnLogin").Click();
Now, in order to download the report I need to extract a number from the HTML body and insert it into a variable. The reason I'm doing that is because this number changes every time I access the page. Please, see the following image, where the number is located inside the HTML Body of the webpage. It's always 12 digits:
This is my problem. I cannot get this number inside a variable. If I could, then I would finalize the Powershell code with the script below.
$output = "C:\Users\AlexSnake\Desktop\WeeklyReport\ReportName.pdf"
Invoke-WebRequest -Uri http://www.exemple.com.br/pdf_pub/xxxxxxxxxxxx.pdf -OutFile $output
Where you see 'xxx..' I would replace for the variable and download the report
After this bit of your code
while($ie.ReadyState -ne 4) {start-sleep -m 100}
Try this:
$($ie.Document.getElementsByTagName("a")).href | ForEach {
# The next line isn't necessary, but just to demonstrate iterating through all the anchor tags in the page (feel free to comment it out)
Write-Host "This is the href tag that I'm enumerating through: $_"
# And this bit checks for that number you're looking for and returns it:
if( $_ -match "javascript:openwindow('/\.\./\.\./[\d+]\.pdf'.*)" )
{
$matches[1]
}
}
This should work.
See the code below with the answer for my question.
$($ie.Document.getElementsByTagName("a")).href | ForEach {
if( $_ -match '(\d+)\.pdf' )
{
$matches[1]
}
}
Thanks!
My problem statement goes like this - I need to pull all hotel names and corresponding price from a web portal. If not via script, this is a tedious manual process for me.
For example on following URL I need name of all hotels with corresponding prices : http://hotel.makemytrip.com/makemytrip/site/hotels/search?session_cId=1403778791562&city=SLV&country=IN&checkin=06282014&checkout=06302014&area=&roomStayQualifier=1e0e&type=&sortName=&searchText=&isBaitNWait=null&fullSearch=false
Desired Output :
Hotel Name Price
Oberoi Wildflower Hall 16,500
Hotel Chaman Palace 1,879
I am doing it in Powershell language. Basically I need to understand how to get value of one placeholder (hotel name or price).So far I have tried this.
$surl="http://hotel.makemytrip.com/makemytrip/site/hotels/search?session_cId=1403778791562&city=SLV&country=IN&checkin=06282014&checkout=06302014&area=&roomStayQualifier=1e0e&type=&sortName=&searchText=&isBaitNWait=null&fullSearch=false"
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($surl)
$doc = $ie.Document
$element = $doc.getElementsByClassName("hotelImgLkflL")
$element > d:\element.txt
However, I am getting following error message.
You cannot call a method on a null-valued expression.
Update : Now I am trying to do it via $web.DownloadString and figured out that the source has following pattern for all Hotel Names :
id="200701171240402395" title="Oberoi Wildflower Hall" href="/makemytrip/site/hotels/detail?
id="201111211716292072" title="Hotel Chaman Palace" href="/makemytrip/site/hotels/detail?
id="200701121106345886" title="Hotel Baljees Regency" href="/makemytrip/site/hotels/detail?
How can I proceed now ? Thanks.
Appreciate any guidance.
Navigate() runs asynchronously, so you need to wait until the website is loaded completely before you can work on it:
...
$ie.navigate($surl)
while ( $ie.ReadyState -ne 4 ) { Start-Sleep -Milliseconds 100 }
$doc = $ie.Document
...
I have this PS script it logins to a site and then it navigate's to another page.
I want to save whole source for that page. but for some reason. some parts of source code is not coming across.
$username = "myuser"
$password = "mypass"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("http://www.example.com/login.shtml")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("username").value = "$username"
$ie.document.getElementById("pass").value = "$password"
$ie.document.getElementById("frmLogin").submit()
start-sleep 5
$ie.navigate("http://www.example.com/thislink.shtml")
$ie.Document.body.outerHTML | Out-File -FilePath c:\sourcecode.txt
Here is pastebin of code which is not coming across
http://pastebin.com/Kcnht6Ry
After you navigate, check for the Ready State again instead of using a sleep. The same code that you had will work.
It appears after running the code, the sleep may not be long enough if the site is slow to load.
while($ie.ReadyState -ne 4) {start-sleep -m 100}
It also looks like there is another post regarding this
innerHTML converts CDATA to comments It looks like some one created a function on that page where you can clean it up. It would be something like this once you have the function declared in your code
htmlWithCDATASectionsToHtmlWithout($ie.Document.body.outerHTML) | Out-File -FilePath c:\sourcecode.txt
I agree with #tkrn regarding using the while loop to wait for IE document to be ready. And for that I recommend to use at least 2 seconds inside the loop.
while($ie.ReadyState -ne 4) {start-sleep -s 2}
Still I found an easier way to get the whole HTML source page exactly from the URL. Here it is:
$ie.Document.parentWindow.execScript("var JSIEVariable = new XMLSerializer().serializeToString(document);", "javascript")
$obj = $ie.Document.parentWindow.GetType().InvokeMember("JSIEVariable", 4096, $null, $ie.Document.parentWindow, $null)
$HTMLDoc = $obj.ToString()
Now, $HTMLDoc has the whole HTML source page intact and you can save it as html file.