2017-10-20 1 views
1

私はこの要素を選択する方法を理解しようとしています。これは、Webページ上のクラスの最後の反復です。 IDは毎週変わります。ソースは以下の通りです: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_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") 

注:私が成功した場合、私の試みは唯一のそれらの条件の下での最初の要素を発見しただろうが、それも見つけることができなかった、という実感を要素は私が試したものに基づいています。要素を見つける方法を見つけ出すと、そのページの最後の反復を見つけるのにも助けが必要です。

+0

私はこの自分自身をテストすることができますので、あなたが偶然にURLを持っていますか? BeautifulSoupで提供したコードを使用して、そのクラスのすべての反復を見つけて最後のものを見つけることができます。しかし、私はURLなしでセレンでテストすることはできません。 – jarcobi889

+0

@ jarcobi889、ウェブサイトでは、ウェブページを閲覧できるようにログイン情報が必要です。スカイプの情報やEメールを交換して話すことができます。しかし、私はここにログイン資格情報を投稿することはできません。 – AP1

答えて

0

おおよそdriver.find_elements_by_class_name("cls_arw arw_blue")[-1]は、単なる要素ではなく複数の要素であることに注意してください。

+0

"List index of range"エラーが表示されています。 – AP1

0

質問ごとに、目的の要素をすべて見つけて識別し、click()メソッドを呼び出すことができます。次のように私たちは、より一般的なことができ

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

:あなたが探ししようとしているとして、あなたはxpathと、以下のオプションを使用することができcls_arw arw_blue

さて、/クラスの最後のインスタンスを識別

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

クラス名のスペースが許可されているかどうか分かりません(ページソースやwebdriverがここで間違っているとは言えません)。とにかく、 ""でクラス名の検索で ""を置き換えてください。 それを動作させる。 jarcobi889、 "find_element _" とあなたの次のステップになり、最後のものを選ぶことにより、上記の言ったように

driver.find_elements_by_class_name('cls_arw.arw_blue')

そして、。

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

関連する問題