如何使用Selenium单击多个加号按钮并显示其全部内容

时间:2018-06-20 21:16:08

标签: python-3.x selenium

我想单击并打开多个加号按钮并打印出其文本。这是我的代码:

from selenium import webdriver
chrome_path=r"G:\My Drive\chrome_driver\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(chrome_path)
driver.get('https://meshb.nlm.nih.gov/treeView')
for links in driver.find_elements_by_css_selector('a.ng-scope'):
    links.find_element_by_xpath("following-sibling::span").click();
    for sublinks in links.find_elements_by_xpath("//*[@class='ng-scope']/span"):
        print(sublinks.text)

这就是我所看到的!节点的文本重复!并且代码无法打开所有加号按钮。仅打开了第一级加号按钮,如何更改代码以打开所有加号按钮,并且一旦打开每个加号按钮,我就会看到节点的文本以及与之关联的整个内容? html的一部分看起来像这样:

<a class="ng-scope">
   <span class="ng-binding ng-scope">Anatomy [A]</span>
</a>
    <ul class="treeItem ng-scope">
        <li class ="ng-scope" >
              < a  class ="ng-scope" href="/record/ui?ui=D001829" >
              < span  class ="ng-binding ng-scope" > Body Regions[A01] < / span >
              </a>
        </li>
        < li class ="ng-scope" >
              <a  class ="ng-scope" href="/record/ui?ui=D001829" >
                < span  class ="ng-binding ng-scope" > Cardio Vascular< / span >
              </a>
                    <ul class="treeItem ng-scope">
                        <li class="ng-scope">
                           <a class="ng-scope" href="/record/ui?ui=D015824">
                           <span class="ng-binding ng-scope">Blood-Air Barrier [A07.025]</span>
                           </a>
                                 <ul class="treeItem ng-scope">                    
                                   <li class="ng-scope">
                                       <a class="ng-scope" href="/record/ui?ui=D018916">
                                       <span class="ng-binding ng-scope">Blood-Aqueous Barrier [A07.030]</span>                        
                                       </a>
                                    </li>
                                 </ul>
                        </li>
                    </ul>
        </li>
    </ul>

enter image description here

0 个答案:

没有答案