简体   繁体   中英

Using Selenium Webdriver in C#, how do I select a text box to write in, then write in it?

Have a script to go to the website. Now I want to login and proceed to next screen. Can't find code on how to go to 'username:' text box, then 'password:' text box.

You will need to give us some HTML of the page, but given a password textbox like this:

<input type="password" id="passwordTextBox">

I'd find it using Selenium's WebDriver like so:

IWebDriver firefoxDriver = new FirefoxDriver();
IWebElement passwordTextBox = firefoxDriver.FindElement(By.Id("passwordTextBox"));

I would then 'write' into it like so:

passwordTextBox.Clear();
passwordTextBox.SendKeys("password");

I would take a look at the Selenium Web Driver documentation, and ask any questions after you've read through it all:

http://seleniumhq.org/docs/03_webdriver.html

//Textbox  id is "username"
IWebDriver driver = new ChromeDriver();    
string url = "https://www.google.com";
IWebElement textBox;
driver.Navigate().GoToUrl(url);
textBox = driver.FindElement(By.Name("username"));
textBox.SendKeys("Test text");
driver.FindElement(selectBy(controlToFind, search)).Click();

Just need to use this code.

  • driver = your selenium web driver
  • SelectBy(controlToFind) = this is your control, xpath code, CSS selector or class.
  • .Click() = Represents the action to do, you can use .click() , .SendKeys() , wait() ...

To Type the value in User Name and Password Text Box. Try below

driver.FindElement(By.Id("\\UserName Text Box ID\\").SendKeys("UserName Text");

driver.FindElement(By.Id("\\Password Text box ID\\").SendKeys("Password Text");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM