[英]Cannot get drop-down options when using Selenium webdriver Java?
[英]Selenium Access options for input drop-down list
我有一個作為只讀下拉列表的輸入控件(Svelte 是它背后的框架)。 請問如何獲取使用 Selenium 和 Java 的下拉選項列表? 我嘗試了選擇選項:
Select allOptions = new Select(webDriver.getWebDriver().findElement(By.xpath(xpath)));
我有例外說你不能在輸入上選擇。 因為該控件是只讀的,所以您不能在其中輸入值。
<input readonly="true" autocapitalize="none" autocomplete="off" autocorrect="off" spellcheck="false" tabindex="0" type="text" aria-autocomplete="list" id="select-instances" placeholder="Select an instance" class="svelte-abcd" style="">
我在這里研究了答案,例如這個。 您不會從 Svelte 獲得列表標簽。 任何建議或幫助將不勝感激。
打開 DevTools -> 源
點擊輸入->在瀏覽器中按F8停止JS執行
檢查下拉選項 -> 寫下 xpath
public List<string> GetOptionsText(IWebDriver driver) { string parentInputXpath = "inputXpath"; string optionXpath = "optionXpath"; List<string> optionsText = new List<string>(); driver.FindElement(By.XPath(parentInputXpath)).Click(); options = driver.FindElement(By.XPath(parentInputXpath)).FindElements(By.XPath(optionXpath)).ToList(); if (options.Count == 0) throw new NoSuchElementException("Dropdown options not found"); foreach (var option in options) { optionsText.Add(option.Text); } return optionsText; }
PS:在這里添加隱式/顯式等待也很好。
我會嘗試使用 sendKeys 組合(您可以嘗試使用 ArrowDown、Tab、Enter),然后如果它不適合您 - 使用 JavaScript。 您可以設置文本,然后在需要時觸發 onchange 事件。
document.getElementById("select-instances").value = "My value";
或設置占位符
document.getElementById("select-instances").setAttribute('placeholder','My value');
If Possible can you share pic of html code of the dropdown element
Select allOptions = new
Select(webDriver.getWebDriver().findElement(By.xpath(xpath)));
List<WebElement> elements = allOptions.getOptions();
List<String> options = new LinkedList<String();
for(WebElement el: elements)
{
options.Add(el.getText());
}
雖然我不是硒專家,但只是嘗試解決這個問題。 我創建了一個 index.html 文件以從下拉菜單中獲取選項。 我只在這個 index.html 文件中創建了一個下拉菜單,它的代碼是這樣的:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="row">
<span class="label">Select Text</span>
<span class="box"><input readonly="true" autocapitalize="none" autocomplete="off" autocorrect="off" spellcheck="false" tabindex="0" type="text" aria-autocomplete="list" id="datatext" placeholder="Select an instance" class="datatext" >
<select class="contentselect"
id="contentselect">
<option></option>
<option value="one">test1</option>
<option value="two">test2</option>
<option value="two">test3</option>
<option value="two">test4</option>
<option value="two">test5</option>
<option value="two">test6</option>
</select></span>
</div>
<script>
textfield = document.getElementById("datatext");
contentselect = document.getElementById("contentselect");
contentselect.onchange = function () {
var text = contentselect.options[contentselect.selectedIndex].value
if (text != "") {
textfield.value += text;
}
}
</script>
</body>
</html>
然后我創建一個 java 文件來讀取 index.html 中的所有選項。 Java 代碼如下所示:-
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\Goal Projects\\Webnovel API\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://127.0.0.1:5500/index.html");
Select allOptions = new Select(driver.findElement(By.xpath("//select[@id='contentselect']")));
List<WebElement> elements = allOptions.getOptions();
List<String> options = new ArrayList<String>();
for (WebElement element : elements) {
options.add(element.getText());
}
for (String s : options) System.out.println(s);
}
結果輸出如下:-
我認為您使用了錯誤的 xpath 您正在使用輸入 xpath,但您應該使用選擇路徑。 我希望這會對你有所幫助,感謝閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.