Python - 如何选择此元素?

时间:2017-10-20 21:16:50

标签: python css selenium webdriver

我正在试图弄清楚如何选择这个元素。这是网页上课程的最后一次迭代。身份证每周都会改变。来源如下:

</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 15, 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 03, 2017 06:00 PM</td><td>15</td><td>Oct 05, 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="48178" />
                            <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/15/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 22, 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 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$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="48344" />
                            <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/22/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><tr>
        <td>Disney Mainline</td><td>
                            <a id="ctl00_cphMain_gdvSchdRel_ctl04_lnkSchTyp" class="cls_arw arw_blue" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl04$lnkSchTyp','')"></a>
                        </td><td>Oct 29, 2017</td><td>
                            <a id="ctl00_cphMain_gdvSchdRel_ctl04_lnkSchDist" class="cls_arw arw_grey" href="javascript:__doPostBack('ctl00$cphMain$gdvSchdRel$ctl04$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$ctl04$hdnApp_Id" id="ctl00_cphMain_gdvSchdRel_ctl04_hdnApp_Id" value="138" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl04$hdnApp_Sch_Id" id="ctl00_cphMain_gdvSchdRel_ctl04_hdnApp_Sch_Id" value="48471" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl04$hdnApp_Grp_Id" id="ctl00_cphMain_gdvSchdRel_ctl04_hdnApp_Grp_Id" value="686" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl04$hdnWk_Strt_Date" id="ctl00_cphMain_gdvSchdRel_ctl04_hdnWk_Strt_Date" value="10/29/2017 00:00:00" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl04$hdnOffset" id="ctl00_cphMain_gdvSchdRel_ctl04_hdnOffset" value="-300" />
                            <input type="hidden" name="ctl00$cphMain$gdvSchdRel$ctl04$hdnPrivType" id="ctl00_cphMain_gdvSchdRel_ctl04_hdnPrivType" value="e" />
                        </td>
    </tr>

我想选择类“cls_arw arw_blue”的最后一次迭代,以便它将我带到指定的链接。但我无法弄明白。

我试过了:

driver.find_element_by_class_name("cls_arw arw_blue")
driver.find_element_by_id("ctl00_cphMain_gdvSchdRel_ctl04_lnkSchTyp")
driver.find_element_by_css_selector("a.cls_arw arw_blue")

注意:我意识到,如果成功,我的尝试只会在这些条件下找到第一个元素,但它甚至无法找到基于我的第一个元素我试过了。当我弄清楚如何找到元素时,我还需要帮助在页面上找到它的最后一次迭代。

3 个答案:

答案 0 :(得分:0)

driver.find_elements_by_class_name("cls_arw arw_blue")[-1]如何注意它的元素不仅仅是元素。

答案 1 :(得分:0)

根据您的问题,我们可以找到/识别所有预期的元素,并在其上调用click()方法。

现在,当您尝试查找/识别班级cls_arw arw_blue的最后一个实例时,您可以将以下选项与xpath一起使用:

driver.findElement(By.xpath("//td[contains(.,'Disney Mainline')]//following::td/a[@class='cls_arw arw_blue'][starts-with(@id,'ctl00_cphMain_gdvSchdRel_ctl')]"));

我们可以更通用如下:

driver.findElement(By.xpath("//td[contains(.,'Disney Mainline')]//following::td[1]/a"));

答案 2 :(得分:0)

我不知道是否允许使用类名中的空格(所以我不能说这里的页面源或webdriver是错误的)。 无论如何,重新调整&#34; &#34;在&#34;中寻找班级名称。&#34;让它发挥作用。

driver.find_elements_by_class_name('cls_arw.arw_blue')

并且,如上所述jarcobi889,&#34; find_element s _&#34;选择最后一个将是你的下一步。

driver.find_elements_by_class_name('cls_arw.arw_blue')[-1]