【发布时间】:2018-04-02 17:05:09
【问题描述】:
我确定有一些简单的事情我没有弄清楚,但我需要帮助。我正在尝试在表格的单元格中选择一个项目,但事实证明这很困难。以下是出处:
<div class="clear formrow top_border">
<div>
<table class="infoGrid" cellspacing="0" cellpadding="0" border="0" id="ctl00_cphMain_gdvSchdRel" style="border-collapse:collapse;">
<tr>
<th scope="col">Program Name</th><th scope="col">
<img alt="Schedule Type" src="../../../../Resources/images/arrow_black_downward.gif" />
</th><th scope="col">Week Of</th><th scope="col">Schedule Distribution</th><th scope="col">Release Date/Time</th><th scope="col">Init Cap</th><th scope="col">CapLift Date/Time</th><th scope="col">Max Cap</th><th scope="col"> </th>
</tr><tr>
<td>Disney Mainline</td><td>
<a id="ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp" class="cls_arw arw_blue" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl02$lnkSchTyp','')"></a>
</td><td>Oct 22, 2017</td><td>
<a id="ctl00_cphMain_gdvSchdRel_ctl02_lnkSchDist" class="cls_arw arw_grey" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl02$lnkSchDist','')"></a>
</td><td>Oct 10, 2017 10:21 AM</td><td>15</td><td>Oct 12, 2017 03:30 PM</td><td>80</td><td>
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnApp_Id" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnApp_Id" value="138" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnApp_Sch_Id" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnApp_Sch_Id" value="48344" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnApp_Grp_Id" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnApp_Grp_Id" value="686" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnWk_Strt_Date" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnWk_Strt_Date" value="10/22/2017 00:00:00" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnOffset" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnOffset" value="-300" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl02$hdnPrivType" id="ctl00_cphMain_gdvSchdRel_ctl02_hdnPrivType" value="e" />
</td>
</tr><tr>
<td>Disney Mainline</td><td>
<a id="ctl00_cphMain_gdvSchdRel_ctl03_lnkSchTyp" class="cls_arw arw_blue" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl03$lnkSchTyp','')"></a>
</td><td>Oct 29, 2017</td><td>
<a id="ctl00_cphMain_gdvSchdRel_ctl03_lnkSchDist" class="cls_arw arw_grey" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl03$lnkSchDist','')"></a>
</td><td>Oct 17, 2017 06:00 PM</td><td>15</td><td>Oct 19, 2017 03:30 PM</td><td>80</td><td>
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnApp_Id" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnApp_Id" value="138" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnApp_Sch_Id" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnApp_Sch_Id" value="48471" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnApp_Grp_Id" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnApp_Grp_Id" value="686" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnWk_Strt_Date" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnWk_Strt_Date" value="10/29/2017 00:00:00" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnOffset" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnOffset" value="-300" />
<input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl03$hdnPrivType" id="ctl00_cphMain_gdvSchdRel_ctl03_hdnPrivType" value="e" />
</td>
</tr>
</table>
</div>
</div>
正如您所知道的,由于表尚未关闭,因此还有更多行。它们看起来完全一样,只是 id 不同。这些是我迄今为止尝试过的事情:
elem = driver.find_element_by_id("ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp")
elem.click()
elem=driver.find_element_by_css_selector("a.cls_arw arw_blue")
elem.click()
for row in driver.find_elements_by_css_selector("a.cls_arw arw_blue"):
cell = row in row.find_elements_by_class_name("cls_arw arw_blue")
cell.click()
elem = driver.find_elements_by_class_name("cls_arw arw_blue")[-1]
elem.click()
我尝试的最后两件事是因为正如我所指出的,表中有更多行看起来完全相同,只是 id 不同。我想总是抓住表格中的最后一个蓝色箭头,但此时我很乐意弄清楚如何单击任何蓝色箭头。您可以在源代码中看到蓝色箭头有一个 href。网页上的表格图片如下。
第一个蓝色箭头是代码的一部分:
<td><a id="ctl00_cphMain_gdvSchdRel_ctl02_lnkSchTyp" class="cls_arw arw_blue" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl02$lnkSchTyp','')"></a></td>
如果您需要更多信息或我需要澄清一些问题,请告诉我。
【问题讨论】:
-
您正在使用 findelements(复数形式)并在列表上调用 click 方法。应该在 web 元素上调用 click。使用单数 findelement 或从列表中检索一个元素,然后调用 click()
-
此外,在你修复
elem.click()之后,我很确定你会在cell.click()上得到错误。因为您的cell似乎是布尔值,而不是 WebElement -
@Grasshopper,对不起......这是一个错字。我在前两次尝试中使用了 find_element 。我会编辑,但我不确定如何编辑。
-
@Andersson,你看到了什么让你认为单元格是布尔值?
-
@AP1,像
something in list这样的表达式返回True或False。你的意思是cell = [row for row in row.find_elements_by_class_name("cls_arw arw_blue")][0]?
标签: python html css selenium selenium-webdriver