我想使用selenium和python对这个站点进行爬网:https://ntrl.ntis.gov/NTRL
但是当我想更改下拉列表的年份时,它就不能工作了。在
下面是它的HTML:
<div id="advSearchForm:FromYear" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all" style="min-width: 63px;">
<div class="ui-helper-hidden-accessible">
<input id="advSearchForm:FromYear_focus" name="advSearchForm:FromYear_focus" type="text" autocomplete="off" role="combobox" aria-haspopup="true" aria-expanded="false" readonly="readonly" aria-autocomplete="list" aria-owns="advSearchForm:FromYear_items" aria-activedescendant="advSearchForm:FromYear_0" aria-describedby="advSearchForm:FromYear_0" aria-disabled="false">
</div>
<div class="ui-helper-hidden-accessible">
<select id="advSearchForm:FromYear_input" name="advSearchForm:FromYear_input" tabindex="-1">
<option value="*" selected="selected"><1900</option>
<option value="1900">1900</option>
<option value="1901">1901</option>
<option value="1902">1902</option>
<option value="1903">1903</option>
</select>
</div>
<label id="advSearchForm:FromYear_label" class="ui-selectonemenu-label ui-inputfield ui-corner-all"><1900</label>
<div class="ui-selectonemenu-trigger ui-state-default ui-corner-right">
<span class="ui-icon ui-icon-triangle-1-s ui-c"/>
</div>
</div>
以下是我的代码:
^{pr2}$但也有例外:
Element is not currently visible and may not be manipulated
我尝试使用js脚本:
driver.execute_script("document.getElementById('advSearchForm:FromYear_input').options[2].selected = 'true'")
但是它也不起作用,我测试了select.select_by_value(xxx)
可以在其他下拉列表中使用,所以这可能是{
我建议使用
click
事件单击元素(id为“advS”的Select
元素earchForm:FromYear_输入“)首先,然后是ExplicitWait event以等待元素可见,然后您应该能够使用select_by_value
方法更改年份。在另外,我将避免使用XPath而使用CSS selector,更好的做法是创建一个Page Object Model,以减少在将来更新页面时保持工具正常工作所需的工作量。在
很抱歉,我不能提供更多帮助,我对python不太熟悉。在
你也可以参考this question.
编辑
看起来好像是在使用
option
中的option
项作为主列表,而实际的选择发生在页面下方的另一个元素中。这个元素是用Javascript动态构建的,所以我在评论中的建议行不通。在我用C语言编写了一个工作应用程序,让您了解您需要做什么:
=================================================================================
编辑2
包括python答案,我以前从来没有写过python,但这似乎是可行的。我强烈建议查看我在上面发布的关于使用PageObject模型和显式等待的链接,以及避免使用XPATH选择器。在
^{pr2}$相关问题 更多 >
编程相关推荐