簡體   English   中英

Select 來自標簽類型 - 使用 selenium C# webdriver 輸入下拉列表

[英]Select from tag type - input dropdown with selenium C# webdriver

掉落

我如何從dropdwon下面獲取元素,我已經使用Select命令到select下拉列表,但這里輸入的是類型。

<input class="tp-select-input" autocapitalize="none" autocorrect="off" autocomplete="off" spellcheck="false" data-testid="register-country" value="">

由於下拉列表中的 select 值不是 select 類型標簽,因此 Selenium Select 方法無法對其起作用。 您必須按照以下步驟操作:

  1. 單擊下拉菜單/或箭頭
  2. 從下拉列表中找到您想要 select 的網絡元素
  3. 使用 java 腳本執行器點擊你想要的選項 select

由於您沒有給出完整的 HTML,下面是偽代碼:

driver.FindElement(By.XPath("//input[@data-testid='register-country']")).Click() // Click on dropdown
 IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
 executor.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[text()='Austria']"))); //You can give more accurate xpath for country you want to select based on other HTML attributes

您也可以嘗試使用以下代碼

IWebElement element = driver.FindElement(By.ClassName("tp-select-input")); driver.FindElement(By.XPath("//span[text()='Australia']")); element.SendKeys(OpenQA.Selenium.Keys.Down); element.SendKeys(OpenQA.Selenium.Keys.Return);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM