Selenium executeScript 没有返回列表。以下代码尝试在电子邮件的主题字段中写入内容,将其保存为草稿,然后通过搜索字段搜索草稿。
((JavascriptExecutor) driver).executeScript("document.getElementById('NewMessage').click();");
Thread.sleep(4000);
((JavascriptExecutor) driver).executeScript("document.getElementById('fSubject').value = \""+ test +"\" ;");
((JavascriptExecutor) driver).executeScript("document.getElementById('SaveDraft').click();");
Thread.sleep(2000);
((JavascriptExecutor) driver).executeScript("document.getElementById('c_clogot').click();");
((JavascriptExecutor) driver).executeScript(
"var f = new FocusEvent('focus');"
+ "var m = new MouseEvent('click');"
+ "var q = document.getElementsByName('query')[0];"
+ "var b = document.getElementsByClassName('c_search_go c_search_psb_go')[0];"
+ "q.dispatchEvent(f);"
+ "q.dispatchEvent(m);"
+ "q.value = \""+ test +"\" ;"
+ "b.dispatchEvent(m);");
Thread.sleep(2000);
以下 javascript 正在获取具有类“Fmd”的所有元素,但 JavaScriptExecutor 未返回 WebElements 列表。
String scriptToExec = "return document.getElementsByClassName('FmD');";
List<WebElement> resultsList = (List<WebElement>) ((JavascriptExecutor) driver)
.executeScript(scriptToExec);