0
我想找出一個解決方案,以便使用硒的所有元素在稍後填充測試值。我能夠獲得所有元素,但我想過濾掉那些以字母's'開頭的元素。所以我可以得到像sFirst,sLast,sAddress等元素。我可以使用下面的代碼。但我不確定這裏的'後'應該是什麼。通過使用Selenium以字母開頭來過濾DOM元素
// I am not sure what should be after 's' here.
List<WebElement> allEle = driver.findElements(By.id("s *"));
OR
// instead of all elements, need to find ids starting with 's'
List<WebElement> allEle = driver.findElements(By.cssSelector("*"));
//iterate and print
for (WebElement ele : allEle) {
ele.getAttribute("id"); // id of each element
ele.getText();
System.out.println ("Element :" + ele.getAttribute("id") + ": Test :" +ele.getText());
}
編輯注:增加了對循環結束的代碼塊中括號
This works,thank you。我試圖過濾出以s +開頭的元素[全部以大寫字母開頭]。我試過這個(「[id^='s [A-Z]']」)),沒有工作。任何建議? – SamK