有没有办法用 Java 在 Selenium Webdriver 中调用 nextElementSibling?
Is there a way to call nextElementSibling in Selenium Webdriver with Java?
我正在处理一个非常混乱的页面结构,我卡在上面的片段看起来像这样:
<div>
<h3>...</h3>
<ul>...</ul>
<h3>...</h3>
<ul>...</ul>
...
</div>
我想获取 <ul>
元素之一,这样我就可以更深入地挖掘它并从那里检索我真正需要的实际值(它里面有一个 table)。目前,我能够获得我正在寻找的 <ul>
之前的 <h3>
元素。由于 ul-elements 没有任何我可以用来直接获取它们的唯一标识符,我希望通过获取 h3-tag 之后的元素(在同一级别)来实现它。有没有办法得到似乎是 nextElementSibling 的东西?
谢谢!
注意! h3 和 ul 元素没有严格的序列号 - 在它们之前可能有也可能没有一些元素,因此获得第 n 个子元素似乎不是一个选项。
您可以使用 xpath 或执行一些 javascript.
来实现此目的
Xpath:
driver.find(By.xpath("//div/h3/following-sibling::ul[1]"));
JavaScript:
JavaScriptExecutor jsExec = (JavaScriptExecutor)driver;
WebElement ulElement = jsExec.executeScript("return arguments[0].nextSibling;", driver.find(By.cssSelector("div h3")));
希望对您有所帮助!
我正在处理一个非常混乱的页面结构,我卡在上面的片段看起来像这样:
<div>
<h3>...</h3>
<ul>...</ul>
<h3>...</h3>
<ul>...</ul>
...
</div>
我想获取 <ul>
元素之一,这样我就可以更深入地挖掘它并从那里检索我真正需要的实际值(它里面有一个 table)。目前,我能够获得我正在寻找的 <ul>
之前的 <h3>
元素。由于 ul-elements 没有任何我可以用来直接获取它们的唯一标识符,我希望通过获取 h3-tag 之后的元素(在同一级别)来实现它。有没有办法得到似乎是 nextElementSibling 的东西?
谢谢!
注意! h3 和 ul 元素没有严格的序列号 - 在它们之前可能有也可能没有一些元素,因此获得第 n 个子元素似乎不是一个选项。
您可以使用 xpath 或执行一些 javascript.
来实现此目的Xpath:
driver.find(By.xpath("//div/h3/following-sibling::ul[1]"));
JavaScript:
JavaScriptExecutor jsExec = (JavaScriptExecutor)driver;
WebElement ulElement = jsExec.executeScript("return arguments[0].nextSibling;", driver.find(By.cssSelector("div h3")));
希望对您有所帮助!