0

I am trying to automate log-in process to a web-application using selenium (in chrome). When the page is loaded there is a popup window which asks login credentials.

I have tried the following.

  1. Pass the username password in the url - Doesnt work that way.
  2. access by name that we get from the developer tool of chrome. -- The popup window part doesn't have a name. It is a .swf component where 'Allowscriptaccess' property is set as 'sameDomain'( I don't know whether this is relevant.) Basically that popup is a .swf component is what I understood.

Is there a way I can access this window and enter the login details in selenium. Thanks in advance.

1 Answer 1

1

Directly submitting the form

You're probably unable to pass the username/password in the URL due to the fact that the server-side implementation expects the Content-Type to be application/x-www-form-urlencoded, multipart/form-data or application/json.

Instead of using https://mywebsite.com/login?username=myusername&password=mypassword you probably need to use something like:

Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: mywebsite.com
Connection: close
User-Agent: Some User Agent
Content-Length: xxx

username=myusername&password=mypassword

The User-Agent header is often necessary to not get blocked on the server side (403 Forbidden).

One way of actually checking this is to use Firebug or Chrome Developer tools to check how the login data will be sent to the server and mimic that behavior.

Using Selenium with HTML form

Using Selenium you can also fill in a form.

If it opens in a new window/tab/popup, you can select the right window. Assuming driver is the WebDriver you're using.

ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());


for (String tab : tabs) {
    
    driver.switchTo().window(tab);

    // Either get it by its url
    if (driver.getCurrentUrl().equals("https://mywebsite.com/my-login") {
        // Do the login
    }

    // Detect based on HTML on the page
    if (driver.findElement(By.cssSelector(".my-form-class #my-form-id")).size() != 0) {
        // Do the login
    }
}

When the login screen get loaded by JavaScript you can wait for the element to become available in the DOM. Again assuming driver is the WebDriver you're using.

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".my-form-class #my-form-id")));

Now you can actually fill in the form:

driver.findElement(By.cssSelector(".my-form-class .username")).sendKeys("your-actual-username");
driver.findElement(By.cssSelector(".my-form-class .password")).sendKeys("your-actual-password");
driver.findElement(By.cssSelector(".my-form-class .submit-button")).click();

Write code to verify a successful login.

Interacting with Flash

If the login is an actual Flash object, you're probably forced to use the Java Robot class. Thereby performing a sequence of steps:

  • mouseMove to the location of the username/email field
  • mousePress to click the form field
  • keyPress to fill in you username

Repeat for password and submit button. Check if the login actually was successful.

Additionally you can use the conductor framework, which can make testing with Selenium easier.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a ton for the response!
before trying out any of the options you suggested - I forgot to mention one thing here. When I try to access this particular URL via chromeDriver I am getting a page which says 'This plugin is not supported' (looks like a flash failure symol). If I manually access the URL it works fine for me and gives me a popup window for log in.
I cannot use Robot as my target coo-ordinates keep on changing.Its a kind of file upload on desired folders ,the position of which keeps on changing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.