簡體   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