Login to website

Started by skuser11, March 18, 2018, 03:07:22 PM

Previous topic - Next topic

skuser11

Is there a sample script with winbatch that does login to a website? (using an existing browser/chrome)
The website has username and password and Enter button to login.

stanl

At one point I think a contributor did something with Firefox and Selenium and suggested it should work with chrome.

td

Of course, the first question is there some reason why you need to use Chrome instead of skipping the use of a browser altogether or using Windows built-in COM Automation browser control or dotNet classes or the WinInet extender?

If there is some reason for using Chrome you might be able to adapt something used for Firefox.  For example,

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/How~To+Automate~FireFox.txt

Or you could try using the UIAutomation assemblies that are part of the dotNet FCL.  It is a bit of a challenge to code, however.   I think there are examples that automate Firefox on this forum that you can find by searching this site.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Neglected to mention the "Kantu Browser Automation" plugin for Chrome and using WinBatch's WinMacro to record mouse movements and keystrokes.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

skuser11

Thanks for the information, it is helpful. The objective is to login to a website, for a performance test. Will need to login several/times for the test. Just need to automate the login.
Is there a helpful script that shows login to a webpage, login with username and password.

stanl

I would give Selenium a try. Below is C# code that employs the WebDriver. WB (newer versions) can compile and execute C#, but Selenium could also be used as a COM component with VBA code.



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Login_Script {

public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver.exe"); //add chrome driver path (System.setProperty("webdriver.chrome.drive",chrome driver path which you downloaded)

WebDriver driver = new ChromeDriver(); // create object of ChromeDriver

driver.manage().window().maximize(); // maximize the browser window

driver.get("MYURL"); //enter url

driver.findElement(By.id("txtUsername")).sendKeys("MYUSER"); //type textbox's id or name or any locater along with data in sendkeys

driver.findElement(By.id("txtPassword")).sendKeys("MYPASSWORD");

driver.findElement(By.id("btnLogin")).click();

Thread.sleep(2000); //used thread for hold process

driver.quit(); //for close browser
}
}




EDIT: were you to download and set up Selenium and fit the C# code into your environment, you can get help here for running the code in WB

td

Correct me if I am wrong but I don't see anything in the C# code that couldn't be implemented directly in a WinBatch script without needing to compile with C#.   

Also, the "Kantu Browser Automation" plugin for Chrome uses Selenium.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on March 20, 2018, 07:16:53 AM
Correct me if I am wrong

No right or wrong. The only reason I brought up Selenium was because the OP indicated he wanted to test site access with user/pwd - and wasn't Selenium developed for testing. And I meant the C# could be compiled and executed - similar format to Jim's post on the Tech DB to find the current URL in Chrome. Didn't mean to lead the thread astray.

td

A more or less useless example as weak proof of concept - it skips the hard parts.

Code (winbatch) Select
;; Assumes required dlls and driver in the dotnet391 directory.
;; Dlls and driver in directory (all of which may not be needed):
;;    geckodriver.exe  ; Firefox driver.
;;    Selenium.WebDriverBackedSelenium.dll
;;    WebDriver.dll
;;    WebDriver.Support.dll
ObjectClrOption("Appbase", 'C:\Projects\DotNet\Selenium\dotnet391')

;; Load everything to be safe...
ObjectClrOption("use", 'WebDriver')
ObjectClrOption("use", 'WebDriver.Support')
ObjectClrOption("use", 'Selenium.WebDriverBackedSelenium')

objBy = ObjectClrNew('OpenQA.Selenium.By')
objDriver = ObjectClrNew('OpenQA.Selenium.Firefox.FirefoxDriver')

objDriver.Navigate().GoToUrl("http://www.google.com/")
   
;; Find the text input element by its name
objQuery = objDriver.FindElement(objBy.Name("q"))
   
;; Enter something to search for
objQuery.SendKeys("Cheese")

;; Now submit the form. WebDriver will find the form for us from the element
objQuery.Submit()

TimeDelay(2)
objDriver.Quit()

;; Closes opened windows.
objBy = 0
objQuery = 0
objDriver = 0
exit
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl


stanl

Modified Tony's example for Chrome Browser. Attached is the script and two screen shots.  Basically, the script worked with just the basic WebDriver and the Chrome Extension in the AppBase folder. One screenshot shows the files I placed in a folder named selenium. They didn't need to be registered and all I did was download the Nuget C# packages on the Selenium download site and extract with 7-Zip. 

Now although the script ran, I tried it from my work PC and got both a dos and window popup concerning the Chrome Driver (but clicked on windows pop-up and the script proceeded). Assume there is a security setting in my work domain.

Anyway, I remember trying the Selenium COM control years ago with no success, but a lot of what I need to do at work now involves Chrome rather than IE so the ability to use the WB CLR is of interest.

Thanks again, Tony for getting things started....