在 C# 中使用 Selenium Webdriver,如何选择要写入的文本框,然后在其中写入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10557196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:19:41  来源:igfitidea点击:

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

c#visual-studioseleniumwebdriver

提问by lbrown

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.

有一个脚本去网站。现在我想登录并进入下一个屏幕。找不到有关如何转到“用户名:”文本框,然后是“密码:”文本框的代码。

采纳答案by Arran

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

你需要给我们一些页面的 HTML,但给我们一个像这样的密码文本框:

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

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

我会发现它使用 Selenium 的 WebDriver,如下所示:

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:

我会看一下 Selenium Web Driver 文档,并在您通读所有内容后提出任何问题:

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

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

回答by Madhu

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");

回答by Hemlata Gehlot

//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");

回答by Diego Montero

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()...
  • driver= 你的硒网络驱动程序
  • SelectBy(controlToFind)= 这是您的控件、xpath 代码、CSS 选择器或类。
  • .Click()= 代表要做的动作,可以用.click(), .SendKeys(), wait()...