我正在使用 Selenium 和 Splinter 为我的 Web 应用程序运行 UI 测试。我正在为页面上的 View 生成一个随机 ID,并想选择随机 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'
我查看了“readthedocs” ' WebElement 的页面,它具有“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]['数据-view-id']
或者如果您想在 Selenium 中执行此操作:
browser.find_element_by_xpath('//ul[@class="nav"]').find_element_by_xpath('.//a[@href="#"]').get_attribute("数据 View - ID")
关于Python Selenium "WebDriverElement' 对象没有属性 'get_attribute',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43858900/