Search code examples
c#seleniumwebdriverdropdownenter

How to specify ENTER button functionality in Selenium WebDriver code using C#


For practice sake, I'm writing test cases for irctc website, there I need to enter from station place and then respective stations with that code will be displayed as bootstrap dropdown and now i have to select one among them and click enter. Unfortunately there is no enter/submit button for from and to text field, please help me to continue with this test case

Here is my code

IWebElement Fromstn = driver.FindElement(By.XPath("//*[@id='divMain']/div/app-main-page/div/div/div[1]/div[1]/div[1]/app-jp-input/div/form/div[2]/div[1]/div[1]/span/i"));
                 Thread.Sleep(2000);
                  Fromstn.SendKeys("MAQ");
                  Fromstn.Click();
 ```**OR**

Actions builder = new Actions(driver); Actions hover = builder.MoveToElement(driver.FindElement(By.XPath("//*[@id='origin']"))); hover.Build().Perform(); Thread.Sleep(2000); hover.SendKeys("MAQ"); hover.Click();


Solution

  • from input try the below css :

    p-autocomplete#origin input
    

    To input try the below css :

    p-autocomplete#destination input
    

    Code :

    driver.FindElement(By.CssSelector("p-autocomplete#origin input")).SendKeys("MAQ");
    driver.FindElement(By.CssSelector("p-autocomplete#destination input")).SendKeys("some to station");
    

    and if you wanna do Keyboard enter then probably use it with sendkeys():

    something like this :

    driver.FindElement(By.CssSelector("p-autocomplete#origin input")).SendKeys("MAQ" + Keys.RETURN);