Python Selenium "WebDriverElement' 对象没有属性 'get_attribute'

标签 python selenium splinter

我正在使用 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/

相关文章:

python - Pandas:从列表中选择包含任何子字符串的行

python - 有什么快速修改这种字典的方法吗?

java - WebElement.clear() 触发 javascript 更改事件 - 替代方案?

java - 我如何处理 Selenium 和 Java 中的引导日期选择器

Python + split : Error - httplib. BadStatusLine: ''

python - 32 位/64 位系统上的二进制文件?

python - 如何将 float 格式化为 10 或 100 位数?

java - 如何在 Selenium Webdriver 中使用 "Upload Image Button"上传文件/照片

python - Splinter 拯救无形的 html

python - 如何从以前保存的 splinter 实例中在 splinter 中设置 cookie?