Use powershell to open the browser and search
What Will I Learn?
How to use powershell to access the website in a simple way
How to use powershell to open the browser
How to use powershell to view the website
How to use powershell to search what we want
Requirements
Difficulty
- Intermediate
Tutorial Contents
Before contents:
If you do not know any about powershell ,you had better reading this post firstly as well as consulting more from the books
Access the website in a simple way
Using IE's COM object to complete a simple Web automation test is the most compact and inexpensive Web automation test since it does not have to introduce third-party plug-ins or tools. The Windows system comes with the Internet explorer and PowerShell.
Start-Process -FilePath iexplore
- FilePath:Open the process by its file path
- By default.the powershell will open Internet Explorer and ignore the default browser.And it will always open a new monitor of the browser
Start-Process -FilePath iexplore -ArgumentList www.baidu.com
- ArgumentList:Add the parameters to the process.In this ,parameter is the url which to be access
Start-Process -FilePath www.baidu.com
- FilePath :Directly add a parameter to the powershell,which means open a new tag to access the website while you have open your default browser.
- This is different from the last operation
Open browser to view the website as well as searching something
Create a browser object
$ie = new-object -com "InternetExplorer.Application";
$ie.navigate("www.baidu.com");
$ie.Visible=$true;
- new-object : Create a new COM object "IE explorer"
- navigate : View the website
- Visible :Only the value of the parameter "Visible" is true can we see a real time operation on the monitor .Or ,it will go on everything in the background which we can not see it.
Locate the element of the website
$inputs=$ie.Document.getElementsByName('wd')
- getElementsByName :Get the element by the HTML tag name
Input something to search
foreach($element in $inputs){
$element.value="who is cha0s0000"
}
- value:Sent a value to the HTML tag of input,which will lead to the baidu search engine
Start to search
$inputs=$ie.Document.getElementById('su').click()
- getElementById :Get the "search "button by its HTML ID
- click() :Simulate the mouse click
The whole program
function IEexplorer($url)
{
$ie = new-object -com "InternetExplorer.Application";
$ie.navigate($url);
$ie.Visible=$true;
[System.Threading.Thread]::Sleep(2000);
return $ie;
}
$ie = IEexplorer('www.baidu.com')
$inputs=$ie.Document.getElementsByName('wd')
foreach($element in $inputs){
$element.value="steemit cha0s0000"
}
$inputs=$ie.Document.getElementById('su').click()
Check the effect of the program
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thanks
Hey @cha0s0000 I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x