2016-08-16 54 views
1

我正在嘗試從使用Python腳本中硒 按鈕的HTML代碼按鈕式webelement文字看起來像檢索文字:如何從一個按鈕式webelement

<button class="list-item ng-binding" ng-click="selectLineFilter(line)" type="button"> 
<i class="mdi mdi-domain blue" aria-hidden="true"></i> 
12063497545 
</button> 

我怎樣才能檢索文本。我用.text,它返回一個錯誤,因爲「'list'對象沒有屬性'text'」。請幫忙。

回答

1

我認爲你正在使用find_elements返回的單WebElementWebElement.text作品列表中,那麼你應該嘗試如下使用find_element: -

driver.find_element_by_css_selector("button.list-item.ng-binding[type='button']").text 

或者,如果你想找到的所有元素文本同樣的定位,你應該嘗試使用find_elements和下面循環遍歷: -

buttons= driver.find_elements_by_css_selector("button.list-item.ng-binding[type='button']") 
for button in buttons: 
    button.text 
+0

嘿,我還有其他的五行這樣,在點擊一個元素,請幫我找回每個元素的文本。我實際上遍歷xpath來訪問每個元素,當我使用.text那裏我得到上述錯誤。 –

+0

@BhuvaneshwariKumarRaja你應該使用循環再利用'find_elements' ..嘗試更新的答案,讓我知道... :) –

+0

您好我試圖取回完整的文本,然後將它們分成列表。 :) alllinestext = self.driver.find_element_by_css_selector(「。dropdown.line-filter-dropdown.shown」)。text activtext = str(alllinestext) self.textcheckval = activtext.splitlines() –

0

使用.GetAttribute( 「的innerHTML」)

0

您可以使用此Java代碼的概念:

WebDriver driver;

字符串一個= driver.findelement(by.xpath( 「」))的getText(); //使用CSS或id屬性

的System.out.println(a)的//這將打印出現在按鈕上的文字。