0

I'm trying to automate TextNow using Selenium but when I add Username and password, I am getting an error "Username or Password is invalid". But, the same is working fine manually.

below is the code which i tried

 static void Main(string[] args)
        {
            IWebDriver driver;

            driver = new ChromeDriver("cromepath");
            driver.Url = "https://www.textnow.com/messaging";
            driver.Manage().Window.Maximize();
            IWebElement userName = driver.FindElement(By.Id("txt-username"));//txt-password
            IWebElement password = driver.FindElement(By.Id("txt-password"));

            userName.SendKeys("username");
            password.SendKeys("password");

            IWebElement login = driver.FindElement(By.Id("btn-login"));
            login.Click();


        }
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
dev
  • 163
  • 1
  • 11

1 Answers1

3

You mentioned you get an error that username / password are invalid. Are you sure you are sending the right credentials?

Your XPath is correct here, so waiting for element to exist is most likely issue -- textnow.com does take a minute to load. You might also want to clear both WebElements before sending keys.

using OpenQA.Selenium.Support.UI;


// declare wait of 15 seconds
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));

// wait for element to exist, then store it in username variable
var username = wait.Until(drv => drv.FindElement(By.Id("txt-username")));

// clear & send keys
username.Clear();
username.SendKeys("username");

The same approach can be repeated for password.

If you are still getting username / password invalid, and you are sure you are using correct credentials, then it's possible SendKeys() is happening too quickly & keystrokes are not all registering in the input fields..

You can wrap SendKeys() in a method to slowly send keys, in case the input entering too quickly is a problem:

public static void SlowlySendKeys(this IWebElement element, string text)
{
    // first clear element
    element.Clear();

    // slowly send keys, wait 100ms between each key stroke
    foreach (var c in text) {
         element.SendKeys(c);
         System.Threading.Thread.Sleep(100);
    }
}

Then, you can call:

username.SlowlySendKeys("username");

This will slow down key strokes and work around issue where key strokes are sent too quickly.

CEH
  • 5,701
  • 2
  • 16
  • 40