首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用selenium或jquery获取除法中的所有元素

如何使用selenium或jquery获取除法中的所有元素
EN

Stack Overflow用户
提问于 2017-04-10 09:18:45
回答 3查看 6.7K关注 0票数 1

我需要把所有的元素都放在一个部门里。除法根据从下拉列表中选择的值动态加载数据。因此,对于每个选定的值,在除法中有一组不同的元素。这是HTML:

代码语言:javascript
复制
<div id="container1">
<div id="container1.1">
<input id="dropdown" type="select">//dropdown for dynamically selecting elements of container1.2
<options>1</options>
<options>2</options>
</div>
<div id="container1.2">
<input type="text" id="element1">
<button id="element2">
</div>
</div>//for container

selenium代码:

代码语言:javascript
复制
List<WebElement> obj=driver.findElements(By.xpath("//[@id='Container']//*"));//to get all elements in division
        System.out.println(obj.get(0).getTagName());
        System.out.println(obj.get(1).getTagName());

这不能给出所需的结果。我收到了以下错误:

代码语言:javascript
复制
invalid selector: Unable to locate an element with the xpath expression //[@id='Container']//* because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[@id='Container']//*' is not a valid XPath expression.       
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-04-10 10:03:37

首先,正如错误消息所述,xpath表达式无效。它应该是

代码语言:javascript
复制
List<WebElement> obj=driver.findElements(By.xpath("//div[@id='Container']//*"));

或者,如果元素不特定于div,则可以使用通配符//*@id='Container‘。

在容器中列出子元素的其他方法,也许更好一些

  1. List<WebElement> parentElements=driver.findElements(By.xpath("//div[@id='Container']"));
  2. 迭代元素,然后使用parentElement.findElements(By.xpath(".//*"))从parentElement中获取子元素。您可以参考本文档https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#findElements-org.openqa.selenium.By-

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2017-04-10 10:19:50

$('.className').html();

好多了

document.getElementsByClassName('className').innerHTML

票数 0
EN

Stack Overflow用户

发布于 2017-04-10 11:36:21

这给出了div....use "//“中的所有元素,而不是第二次..because,它只给出根类型的元素。

代码语言:javascript
复制
List<WebElement> obj=BaseClass.driver.findElements(By.xpath("//*[@id='Container']//*"));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43319290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档