2017-05-08 93 views
1

我正在使用Selenium和Splinter爲我的Web應用程序運行我的UI測試,我爲頁面上的視圖生成一個隨機ID,並且想要選擇隨機。ID爲測試Python Selenium「WebDriverElement」對象沒有屬性'get_attribute'

這裏是我使用

from selenium import webdriver 
from splinter import Browser 
executable_path = {'executable_path':'./chromedriver.exe'} 
browser = Browser('chrome', **executable_path) 

data_view_id = browser.find_by_xpath('//ul[@class="nav"]').find_by_xpath('.//a[@href="#"]')[0].get_attribute("data-view-id") 
# I am trying to get the id for the first item in the nav list and use it elsewhere 
print(data_view_id) 

這個代碼是我收到的錯誤:我已經看過了「readthedocs

AttributeError: 'WebDriverElement' object has no attribute 'get_attribute' 

'WebElement的頁面,它具有'get_attribute'值。我找不到有關WebDriverElements的任何文檔,並且需要訪問WebElement的幫助來代替

回答

2

WebDriverElement來自Splinter,而不是Selenium。

在分裂,你訪問屬性像字典(見Splinter docs

data_view_id = browser.find_by_xpath('//ul[@class="nav"]').find_by_xpath('.//a[@href="#"]')[0]['data-view-id']

或者,如果你想要做它在硒:

browser.find_element_by_xpath('//ul[@class="nav"]').find_element_by_xpath('.//a[@href="#"]').get_attribute("data-view-id")

相關問題