Python Selenium“WebDriverElement”对象没有属性'get_attribute'

Python Selenium "WebDriverElement' object has no attribute 'get_attribute'

我正在使用 Selenium 和 Splinter 运行 我的 UI 测试我的 Web 应用程序。我正在为页面上的视图生成一个随机 ID,并希望 select 用于测试的随机 ID。

这是我使用的代码

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)

这是我收到的错误:

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

我查看了 WebElement 的“readthedocs”页面,它具有 'get_attribute' 值。我找不到任何关于 WebDriverElements 的文档,需要帮助来访问 WebElement

WebDriverElement 来自 Splinter,而不是 Selenium。

在 Splinter 中,您可以像字典一样访问属性(请参阅 Splinter docs

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

或者如果您想在 Selenium 中执行此操作:

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