简体   繁体   中英

Login in to https website and download files with powershell

I'm looking to make a script that will automate a manual process thats currently being down. To give a background of the process:

  1. User Logs into https website
  2. Clicks a link
  3. Enters date range
  4. Downloads text file
  5. renames text file

From what I can tell when the user clicks the link to download the file, the date range can be manipulated in the URL. For example: https://website/path/report&beginDate=3/18/2016%2000:00&endDate=3/18/2016%2023:59&download

This file download is always done for the day before so if I was doing this on Tuesday, my date range would be for Monday's date.

Schedule:

  • Monday: Download Saturday and Sunday as separate files
  • Tuesday: Download Monday's file
  • Wednesday: Download Tuesday's file
  • Thursday: Download Wednesday's file
  • Friday: Download Thursday's file
  • Saturday: Download Friday's file

So the script would have to query the running PC or some kind of ntp source for what day it is then probably use IF/THEN based on what day it is. It would also need to pass that custom date into the URL to download the file. Then once the file is downloaded, rename it to YYYYMMDD.txt file.

I'd use Selenium . They have a .NET library, which means you can use it with Powershell.

Here is their .NET Documentation . I'd recommend using the chrome driver as it tends to be a bit more reliable.

Essentially, download the .NET library for Selenium (currently v2.53) , and place the necessary .dll files in the same directory as your chromedriver.exe file.

Then, within Powershell, import your DLLs with the Add-Type cmdlet.

From there, you're ready to go!

Here is a sample script that goes to Google and enters a search term.

> Add-Type -Path C:\dev\selenium\*.dll
> $driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 49693
Only local connections are allowed.
> $driver.Navigate().GoToUrl("https://google.com")
> $inputField = $driver.FindElementById("lst-ib")
> $inputField.SendKeys("My Search Term")
> $inputField.Submit()

Although their documentation is a bit hard to navigate, it really is the best way to learn the methods available to you.

Additionally, you can pipe an element that you've already selected into the Get-Member cmdlet to see what methods and properties are available to you.

> $inputField | Get-Member


   TypeName: OpenQA.Selenium.Remote.RemoteWebElement

Name          MemberType Definition
----          ---------- ----------
Clear         Method     void Clear(), void IWebElement.Clear()
Click         Method     void Click(), void IWebElement.Click()
Equals        Method     bool Equals(System.Object obj)
FindElement   Method     OpenQA.Selenium.IWebElement (etc...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM