按文本查找按钮元素

时间:2018-02-06 09:19:01

标签: python python-3.x firefox selenium-webdriver

<button aria-label="Connect with ABC" class="search-result__actions--primary button-secondary-medium m5" data-ember-action="" data-ember-action-1510="1510">Connect</button>

<button aria-label="Connect with DEF" class="search-result__actions--primary button-secondary-medium m5" data-ember-action="" data-ember-action-1467="1467">Invite Sent !</button>

我正在尝试找到带有文字的按钮&#34; Connect&#34;使用

connect_buttons = firefox.find_elements_by_link_text ('Connect')

但是firefox无法找到按钮。

即使我尝试使用以下模式,但

firefox.find_elements_by_xpath("//*[contains(text(), 'Connect')]")
# Not accurate results

firefox.find_elements_by_css_selector('.search-result__actions--primary.button-secondary-medium.m5'); 
# return all button with same css even which doesn't have text "Connect".

请建议。

1 个答案:

答案 0 :(得分:0)

您应该尝试使用relative xpath。尝试以下代码,如果您的剩余代码正确,它应该可以使用

.//*[@class='search-result__actions--primary button-secondary-medium m5']//*[text()='Connect']