繁体   English   中英

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

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

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

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

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

我发现它使用Selenium的WebDriver如下:

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

我会像这样“写入”它:

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

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

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

只需要使用这段代码。

  • driver =你的selenium web驱动程序
  • SelectBy(controlToFind) =这是你的控件,xpath代码,CSS选择器或类。
  • .Click() =表示的动作做,你可以使用.click() .SendKeys() wait() ...

在“用户名”和“密码文本框”中键入值。 试试以下

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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