我使用SeleniumWebDriver测试一个带有下拉菜单的网站,该下拉菜单为不同的用户提供了不同的选项。选项的数量及其值总是不同的。当我查看源代码时,我看到了下面的代码。您能否提供一个示例,说明我如何在Python中刮取它并列出所有可用的选项值
<div _ngcontent-pxo-26="" class="col-md-6">
<div _ngcontent-pxo-26="" class="form-group">
<label _ngcontent-pxo-26="" for="Filter_ClientRegion">Region</label>
<select _ngcontent-pxo-26="" class="form-control ng-pristine ng-valid ng-touched" id="Filter_ClientRegion">
<option _ngcontent-pxo-26="" value="">All</option>
<!--template bindings={}--
<option _ngcontent-pxo-26="" value="A">A</option>
<option _ngcontent-pxo-26="" value="B">B</option>
<option _ngcontent-pxo-26="" value="C">C</option>
<option _ngcontent-pxo-26="" value="D">D</option>
<option _ngcontent-pxo-26="" value="E">E</option>
<option _ngcontent-pxo-26="" value="F">F</option>
<option _ngcontent-pxo-26="" value="G">G</option>
</select>
</div>
</div>
要
select
一个特定的option
,您可以使用以下内容:要获取
option
的values
,可以使用:备注:
1.我无法完全测试上述代码,因为我没有完整的源代码
2.请注意
options
代码位于注释块<! template bindings={}
内,您可能无法检索其值这应该很容易
你可以用BeautifulSoup来做这件事
因为您提到了selenium,所以这段代码首先使用它,以防您需要它通过登录或其他需要selenium的东西。如果您不需要selenium,那么可以跳到使用
BeautifulSoup
生成soup
的行。前面的代码只是展示了如何使用selenium获取源代码,以便BeautifulSoup
可以访问它首先找到包含所有HTML代码的
select
标记,包括注释的内容。然后获取列表中的每个项目,将其转换为字符串,并将其连接为一个大字符串,并在<select>
前加前缀。将这个大字符串转换为soup和findAll
其中的option
标记。从每个标签中显示您想要的任何内容相关问题 更多 >
编程相关推荐