selenium找到没有名称,没有id和没有唯一类的元素

时间:2017-04-04 21:44:58

标签: python selenium selenium-webdriver

我试图抓一个有几个标签的网站可以点击它,但每个可点击的标签都没有唯一的ID,名称或类。

该网站的标签部分看起来非常像:

enter image description here

包含它们的HTML无序列表在网站中编码如下:

<ul class="nav nav-tabs">
    </li><!-- end ngRepeat: tab in tabState.tabs --><li ng-class="{active: active, disabled: disabled}" ng-repeat="tab in tabState.tabs" heading="Movies" active="tab.active" select="select(tab)" class="ng-scope ng-isolate-scope">
        <a href="" ng-click="select()" uib-tab-heading-transclude="" class="ng-binding">Movies</a>
    </li><!-- end ngRepeat: tab in tabState.tabs --><li ng-class="{active: active, disabled: disabled}" ng-repeat="tab in tabState.tabs" heading="Music" active="tab.active" select="select(tab)" class="ng-scope ng-isolate-scope active">
        <a href="" ng-click="select()" uib-tab-heading-transclude="" class="ng-binding">Music</a>
    </li><!-- end ngRepeat: tab in tabState.tabs --><li ng-class="{active: active, disabled: disabled}" ng-repeat="tab in tabState.tabs" heading="Books" active="tab.active" select="select(tab)" class="ng-scope ng-isolate-scope">
        <a href="" ng-click="select()" uib-tab-heading-transclude="" class="ng-binding">Books</a>
    </li><!-- end ngRepeat: tab in tabState.tabs --><li ng-class="{active: active, disabled: disabled}" ng-repeat="tab in tabState.tabs" heading="Magazines" active="tab.active" select="select(tab)" class="ng-scope ng-isolate-scope">
        <a href="" ng-click="select()" uib-tab-heading-transclude="" class="ng-binding">Magazines</a>
    </li>
</ul>

如何使用python / selenium点击所需的标签?让我们想象一下所需的标签是MUSIC。

我试过这样的事情:

myEl = browser.find_element_by_css_selector("a[class='ng-binding']")

但我怎么知道哪些课程正在选择,因为电影,音乐,书籍和杂志都属于'ng-binding'

2 个答案:

答案 0 :(得分:3)

请尝试以下代码:

browser.find_element_by_link_text("Movies")

答案 1 :(得分:0)

您可以使用该选择器获取所有这些元素并循环显示它们&amp;根据文字做出选择。

allTabs = browser.find_elements_by_css_selector("a[class='ng-binding']")

for tab in allTabs:
    if tab.text == 'Movies':
        tab.click()   #Or do something else with it    
        break;