すべての親ノード「// td」をリストに保存したシナリオがあり、リストの各要素の子ノードの詳細をフェッチする必要があります。これが私のコードです:
WebDriver driver = new ChromeDriver();
driver.get("https://jqueryui.com/datepicker/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
driver.switchTo().frame(0);
driver.findElement(By.xpath("//*[@id='datepicker']")).click();
WebElement Calendertable = driver.findElement(By.xpath("//table[@class='ui-datepicker-calendar']//tbody"));
List<WebElement> Date = Calendertable.findElements(By.tagName("td"));
System.out.println(Date.size());
for( WebElement e : Date) {
System.out.println(e.findElement(By.xpath(".//a")).getAttribute("class"));
}
「td」ノードのすべての子の詳細を取得する必要があります。コードを記述しましたが、機能しません。「.//a」要素に対して「NoSuchElementException:」をスローしています。
提案してください
その理由は、一部の<td>
セル(つまり、日付がないテーブルの最初と最後に、サブタグがないためです。a
例:
<td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled"> </td>
したがって、td
nounselectable
またはdisabled
classがあることを確認することで解決できます。
if(!e.getAttribute("class").contains("disabled")) {
System.out.println(
e.findElement(By.xpath(".//a")).getAttribute("class"));
}
しかし、さらに良いことです。複数回検索する代わりに、気になる要素(a
内部td
)だけを1回検索することができます。
List<WebElement> aInDate = driver.findElement(By.xpath("//table[@class='ui-datepicker-calendar']//tbody//td/a"));
for( WebElement a : aInDate) {
System.out.println(a.getAttribute("class")); }
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加