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

本文关键字:在其中 然后 文本 选择 Selenium Webdriver 何选择 | 更新日期: 2023-09-27 18:33:59

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

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

您需要向我们提供一些页面的 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 = 您的硒网络驱动程序
  • 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");