如何在不使用xpath

时间:2017-03-26 20:09:28

标签: selenium

我是selenium的新手,下面是我的HTML,我想展示Dhritrashtra的儿子和pandu的孙子(不使用xpath)。我尝试过像getText和getLinkText这样的方法,但它并不适合我。请帮忙。谢谢。             

  • 库鲁                 
                          
    1. 山塔努                         
                                    
      1. Vichitravirya
      2.                                 
                                            
        1. 持国                                              
          1. 杜尤丹
          2. Dushasana
          3. Dussalan
          4. Jalagandha
          5. 萨马
          6. 萨哈
          7. Vindha
          8. Anuvindha
          9. Durmukha
          10. 奇特雷泽纳
          11. Durdarsha
          12. Durmarsha
          13. < LI> Dussaha
          14. Durmada
          15. Vikarna
          16. Dushkarna
          17. 德德霍拉
          18. Vivinsati
          19. Durmarshana
          20. Durvishaha
          21. Durvimochana
          22. Dushpradharsha
          23. Durjaya
          24. Jaitra
          25. Bhurivala < / LI>
          26. 拉维
          27. Jayatsena
          28. 舒亚塔
          29. Srutavan
          30. Srutanta
          31. Jayat
          32. 奇特拉
          33. Upachitra
          34. Charuchitra
          35. Chitraksha
          36. Sarasana
          37. Chitrayudha
          38. < LI> Chitravarman
          39. Suvarma
          40. Sudarsana
          41. Dhanurgraha
          42. Vivitsu
          43. Subaahu
          44. 南大
          45. Upananda
          46. Kratha
          47. Vatavega
          48. Nishagin
          49. Kavashin
          50. Paasi < / LI>
          51. Vikata
          52. 索马
          53. Suvarchasas
          54. Dhanurdhara
          55. Ayobaahu
          56. Mahabaahu
          57. Chithraamga
          58. Chithrakund翼
          59. Bheemaratha
          60. Bheemavega
          61. Bheemabela
          62. Ugraayudha
          63. Kundhaadhara
          64. Vrindaaraka < / LI>
          65. Dridhavarma
          66. Dridhakshathra
          67. Dridhasandha
          68. Jaraasandha
          69. Sathyasandha
          70. Sadaasuvaak
          71. Ugrasravas
          72. 格拉纳
          73. Senaany
          74. Aparaajitha
          75. Kundhasaai
          76. Dridhahastha
          77. < LI> Suhastha
          78. Suvarcha
          79. Aadithyakethu
          80. Ugrasaai
          81. Kavachy
          82. Kradhana
          83. Kundhy
          84. Bheemavikra
          85. Alolupa
          86. Abhaya
          87. Dhridhakarmaavu
          88. Dhridharathaasraya
          89. Anaadhrushya < / LI>
          90. Kundhabhedy
          91. Viraavy
          92. Chithrakundala
          93. Pradhama
          94. Amapramaadhy
          95. Deerkharoma
          96. Suveeryavaan
          97. Dheerkhabaahu
          98. Kaanchanadhwaja
          99. Kundhaasy
          100. Virajas
                                            
        2.                                   
        3. 潘杜                                             
                                                            
          1. Yudhishtir                                                     
            1. Prativindhya
                                                            
          2.                                                 
          3. 比姆                                                     
                                                                      
            1. Sutasoma
            2.                                                         
            3. Ghatotkch
            4.                                                                                                                                                       
            5. 阿尔琼                                                     
                                                                        
              1. Srutakirti
              2.                                                         
              3. Babhruvahan
              4.                                                                                                                                                       
              5. 纳库尔                                                      
                                                                          
                1. Satanika
                2.                                                                                                                                                       
                3. Sahadev                                                     
                                                                            
                  1. Shrutkarma
                  2.                                                                                                                                                                                                                                                                                     
  • 2 个答案:

    答案 0 :(得分:0)

    以下是您的查询解决方案:

    1. 始终不必使用xpath。根据Selenium文档&amp;标准,按顺序考虑以下属性:id,name,css,linktext,xpath。如果仍然无法检测元素,请尝试使用css / xpath等多个属性,如class,src等。

    2. 一旦你可以识别元素,那么只有你能够检索元素的属性,如getText()&amp; getLinkText()。

    3. 最重要的是,您已从网站上提供了该文本的副本。从网站文本中识别出一个元素来帮助你是不可能的。您需要提供HTML DOM的一些相关部分,以便我们帮助您。您可以查看任何网页的PageSource以了解元素的属性(id / name / css / xpath)。为此,当您在网页上时,可以右键单击并选择&#34;查看页面来源&#34;。对于Mozilla Firefox,您可以下载&amp;安装Firepath&amp;扩展程序等扩展程序Firebug知道元素的属性。

    4. 最后,您必须使用Java / Python / C#编写一些代码,通过您选择的Selenium打开浏览器,打开网站并使用网页上的不同元素执行某些操作。

      < / LI>

      如果这回答了你的问题,请告诉我。

    答案 1 :(得分:0)

    我不知道您的html具有的确切语法,但您可以根据我的html元素假设使用cssSelector,如下所示:

    1)ul ol ol ol li:nth-child(n) - n= element index

    2)ul ol ol ol:nth-child(2) li:nth-child(n) li:nth-child(1) - n= element index