我有以下HTML结构:
<button class="dropdown-toggle selectpicker btn btn-primary" data-toggle="dropdown" type="button" data-id="strategic_reseller_country" title="United States"> <div class="dropdown-menu open" style="max-height: 245.6px; overflow: hidden; min-height: 40px;"> <ul class="dropdown-menu inner selectpicker" role="menu" style="max-height: 243.6px; overflow-y: auto; min-height: 38px;"> <li class="" rel="0"> <a class="" style="" tabindex="0"> <span class="text"/> <i class="glyphicon glyphicon-ok icon-ok check-mark"/> </a> </li> <li rel="1"> <a class="" style="" tabindex="0"> <span class="text">Andorra</span> <i class="glyphicon glyphicon-ok icon-ok check-mark"/> </a> </li> <li rel="2"> <a class="" style="" tabindex="0"> <span class="text">United Arab Emirates</span> <i class="glyphicon glyphicon-ok icon-ok check-mark"/> </a> </li> <li rel="3"> <a class="" style="" tabindex="0"> <span class="text">Afghanistan</span> <i class="glyphicon glyphicon-ok icon-ok check-mark"/> </a> </li> <li rel="4"> <li rel="5"> <li rel="6"> <li rel="7"> <li rel="8"> <li rel="9"> <a class="" style="" tabindex="0"> <span class="text">Netherlands Antilles</span> <i class="glyphicon glyphicon-ok icon-ok check-mark"/> </a> </li> </ul> </div>
那么我如何从清单中获得该物品?
我是Node.Js(JavaScript)的新手,所以我不知道如何实现它,node.Js但是可以在Java中实现,如下所示:
Node.Js
node.Js
Select dropdown = new Select(driver.findElement(By.class("dropdown-menu inner selectpicker"))); dropdown.selectByVisibleText("Andorra");
我会为此使用Cheerio。
module.exports = { "login": function (browser) { var cheerio = require("cheerio") browser .url("http://www.wikipedia.org") .waitForElementVisible('body', 1000) .waitForElementVisible('select#searchLanguage', 1000) .source(function(result) { // .source() dump the page source in the variable $ = cheerio.load(result.value) var num_languages = $('select#searchLanguage option').length console.log('Wikipedia.org Languages: ' + num_languages); }) .end(); } };
或者只是使用.execute()命令
.execute()