Use powershell to open the browser and search

in #utopian-io8 years ago (edited)

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

    GIF.gif

Requirements

  • Windows or other system like linux,macos

  • Powershell

    图片.png

  • Coding IDE

    图片.png

  • .NET framework

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

some powershell foundation


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

图片.png

  • 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

图片.png

  • ArgumentList:Add the parameters to the process.In this ,parameter is the url which to be access
Start-Process -FilePath www.baidu.com

图片.png

  • 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;

图片.png

  • 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')

图片.png

  • getElementsByName :Get the element by the HTML tag name

Input something to search

foreach($element in $inputs){
    $element.value="who is cha0s0000"
 }

图片.png

  • 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()

图片.png

  • 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

GIF.gif



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @cha0s0000 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.035
BTC 108074.45
ETH 4399.12
USDT 1.00
SBD 0.83