【问题标题】:Python Selenium "WebDriverElement' object has no attribute 'get_attribute'Python Selenium“WebDriverElement”对象没有属性“get_attribute”
【发布时间】:2017-05-08 23:49:10
【问题描述】:

我正在使用 Selenium 和 Splinter 为我的 Web 应用程序运行 UI 测试。我正在为页面上的视图生成一个随机 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'

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

【问题讨论】:

    标签: python selenium splinter


    【解决方案1】:

    WebDriverElement 来自 Splinter,而不是 Selenium。

    在 Splinter 中,您可以像 dict 一样访问属性(参见 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")

    【讨论】:

      猜你喜欢
      • 2016-07-28
      • 2021-10-10
      • 1970-01-01
      • 2020-07-06
      • 2018-05-23
      • 2016-11-26
      • 2016-07-21
      • 2012-10-06
      相关资源
      最近更新 更多