繁体   English   中英

Python和Selenium:从表的下拉菜单中选择选项

[英]Python and selenium: choose option from dropdown in table

我还是Python和Selenium的新手。 我想从html表中包含的下拉菜单中选择某个选项。 但是我无法使其正常工作。 我究竟做错了什么? 任何帮助表示赞赏吗?

HTML代码段:

<table class="StdTableAutoCollapse">
<tr>
<td class="StdTableTD150">
<span id="ctl00_ContentPlaceBody_LbLProd1" class="StdLabel150">Prod1:</span>
</td>
<td class="StdTableTD330">
<select name="ctl00$ContentPlaceBody$DropDownListUnitType"            onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceBody$DropDownListUnitType\',\'\')', 0)" id="ctl00_ContentPlaceBody_DropDownListUnitType" class="StdDropDownList330" Class="option">
    <option selected="selected" value="#">- nothing -</option>
    <option value="P">Dummy1</option>

</select>
                        </td>
                    </tr>
                    <tr>

我尝试了以下选择值“ Dummy1”

Python代码:

dropdown1 =     
browser.find_element_by_id('ctl00_ContentPlaceBody_DropDownListUnitType')
select = Select(dropdown1)
select.select_by_value("P")

我想念什么或做错什么? 任何帮助深表感谢。

编辑

我在使用Python 3.6的Anaconda中的IPython控制台上收到错误:

NoSuchElementException: Unable to locate element: 
[id="ctl00_ContentPlaceBody_DropDownListUnitType"]

EDIT2

我检查了问题是否是由于注释和其他关于stackoverflow的问题中提到的iframe不同引起的。 我使用此https://developer.mozilla.org/zh-CN/docs/Tools/Working_with_iframes中提到的想法来检查iframe,并尝试使用Alibabas登录页面示例。 显示了两个不同的iframe。 在我尝试与硒一起使用的页面中,只有一个iframe。

看来,Webdriver很难使用其id直接到达下拉菜单。 您可能需要先找到表格,然后到达下拉菜单。 尝试关注并让我知道,是否可行。

 dropdown1 =     
 browser.find_element_by_xpath("//table[@class='StdTableAutoCollapse']/tr[1]/descendant::select[@id='ctl00_ContentPlaceBody_DropDownListUnitType'][1]")
 select = Select(dropdown1)
 select.select_by_value("P")

问题是我试图在Firefox 45中使用Selenium 3.0.2。这会产生问题,因此我无法选择下拉值。我降级到Selenium 2.5.x,问题消失了。 问题不是我首先想到的是选择在表中。 希望以后对其他人有所帮助。另请参阅以下问题: Python,Firefox和Selenium 3:从下拉列表中选择值不适用于Firefox 45

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM