首页 > 解决方案 > (NodeJS) Selenium Webdriver findElement.By.css() 页面有多个时只返回一个值

问题描述

这是有问题的代码块:

var webdriver = require('selenium-webdriver')
var chrome = require('chromedriver')

const driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build()

driver.get("http://thewebsite.com").then(x => {
  driver.findElement(webdriver.By.css("[title^='ANI:']")).then(selObject => {
     console.log(selObject)
     selObject.getAttribute("title").then(x => console.log(x))
  }) 
})

这是我要提取的页面源:

<tbody id="CONFGROUP_0">               
    <!-- dynamic content here -->
    <tr id="35534" style="background-color: rgb(228, 225, 229);">
    <td nowrap="" valign="middle" height="30" width="210" title="ANI: 7076  DNIS: 7791" style="font-size: 0.8em;"><div style="overflow: hidden; display: inline; float: left; padding-top: 3px; width: 210px;"><div style="display: inline; padding-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100%;">SOC&nbsp;OPS ALERT</div><input type="text" maxlength="50" style="display: none;"></div><div style="display: none; float: right; padding-right: 10px; height: 30px;"><img src="images/talkshow/03_buttonlower_on.gif" style="margin-top: 6px;"></div></td>
    </tr>
    <tr id="35533" style="background-color: rgb(255, 255, 255);">
    <td nowrap="" valign="middle" height="30" width="210" title="ANI: 8017057799  DNIS: 7076" style="font-size: 0.8em;"><div style="overflow: hidden; display: inline; float: left; padding-top: 3px; width: 210px;"><div style="display: inline; padding-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100%;">8017057799</div><input type="text" maxlength="50" style="display: none;"></div><div style="display: none; float: right; padding-right: 10px; height: 30px;"><img src="images/talkshow/03_buttonlower_on.gif" style="margin-top: 6px;"></div></td>
    </tr>
    </tbody>

当我document.querySelectorAll("[title^='ANI:")在 Chrome 控制台中运行时,它会返回我需要的两个元素。但是,当我findElement在上面的代码块中运行 find 时,它只返回一个对象。如果我运行driver.executeScript("document.querySelectorAll(\"[title^='ANI:\")"),它将返回null

我希望findElement它会返回它找到的所有东西,并且从我环顾四周来看,它看起来应该这样做。我什至添加了一个超时,以确保在进行搜索之前页面已完全加载,但仍然没有。

任何帮助将非常感激。

标签: cssnode.jsseleniumselenium-webdriverselector

解决方案


原来findElements存在,我是个白痴。谢谢你们

elements = driver.findElements(webdriver.By.css("[title^='ANI:']"))


推荐阅读