Python如何选择此元素?

2024-09-27 00:12:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在想办法选择这个元素。这是网页上该类的最后一次迭代。身份证每周都会换。资料来源如下:

</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\u arw arw\u 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")

注意:我意识到,如果成功的话,我的尝试只能在那些条件下找到第一个元素,但它甚至无法根据我的尝试找到元素。当我弄清楚如何找到元素时,我还需要帮助在页面上找到它的最后一次迭代。你知道吗


Tags: nameidinputvaluetypehiddentdctl00
3条回答

我不知道类名中是否允许使用空格(所以我不能说这里的页面源或webdriver是否错误)。 无论如何,在按“”搜索类名时重新绘制“”可以使其工作。你知道吗

driver.find_elements_by_class_name('cls_arw.arw_blue')

正如上面jarcobi889所说的,“找到元素”并选择最后一个元素将是您的下一步。你知道吗

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

根据您的问题,我们可以定位/标识所有预期的元素,并对它们调用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"));

那么driver.find_elements_by_class_name("cls_arw arw_blue")[-1]注意它是复数元素而不仅仅是元素。你知道吗

相关问题 更多 >

    热门问题