0

I am trying to access a webpage where I get a pop-up to enter username and password. With the code below I manage to enter the values the values but the problem is that when I first enter the username and then TAB the TAB will actually replace the username then jump to the password field. But then also the password is written in the "user field". The focus jumps back to the username.

var alert = driver.SwitchTo().Alert();
            alert.SendKeys(_credentials.UserName);
            System.Threading.Thread.Sleep(1000);
            alert.SendKeys(Keys.Tab);
            System.Threading.Thread.Sleep(1000);
            alert.SendKeys(_credentials.Password);
            System.Threading.Thread.Sleep(1000);
            alert.Accept();

I have tried with below code as well but that only works for IE not Firefox.

var alert = driver.SwitchTo().Alert();
            alert.SetAuthenticationCredentials(_credentials.UserName, _credentials.Password);
            System.Threading.Thread.Sleep(1000);
            alert.Accept();

Any idea how to come around this issue in Firefox?

ASE
  • 355
  • 2
  • 12
  • Instead of sending a TAB key, you could try moving the focus to the password element directly (in a similar fashion as demonstrated in this question/answer: https://stackoverflow.com/a/11337988) –  Dec 14 '18 at 14:43

1 Answers1

1

Can you try to combine all the SendKeys in one and give it a try?

alert.SendKeys(_credentials.UserName + Keys.TAB + _credentials.Password).Accept();

Also eventually you can try to get the webpage with authentication e.g.:

driver.Navigate().GoToUrl('http://<user>:<pass>@<yourpage>')

You might need to url encode your password if it contains @ character :)

Rain9333
  • 570
  • 4
  • 22
  • 1
    Hi, Thank you for the input. This fixed the problem. alert.SendKeys(_credentials.UserName + Keys.Tab + _credentials.Password); System.Threading.Thread.Sleep(1000); alert.Accept(); Great suggestion! – ASE Dec 17 '18 at 09:10