【问题标题】:Using Selenium WebDriver to retrieve value of a HTML div使用 Selenium WebDriver 检索 HTML div 的值
【发布时间】:2019-01-26 21:10:12
【问题描述】:

我正在尝试使用以下代码从 div 中获取值:

input_field = driver.find_elements_by_xpath("/html/body/div[@id='display']")
input_field_value = input_field.text

HTML:

<div id="display" class="display xh-highlight">1</div>

但我失败了。

【问题讨论】:

  • 任何错误信息?
  • 能否请您添加任何错误代码并提供完整的 HTML 代码。

标签: python selenium selenium-webdriver


【解决方案1】:

试试:

input_field = driver.find_elements_by_xpath("//div[@id='display']")
input_field_value = input_field.text

input_field = driver.find_elements_by_xpath('display')
input_field_value = input_field.text

更多内容可在此链接阅读:https://selenium-python.readthedocs.io/locating-elements.html

【讨论】:

    【解决方案2】:

    你可以试试这个,应该可以的。

    input_field =driver.find_element_by_xpath('//div[@class="display xh-highlight"]')
    input_field_value = input_field.text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 2012-06-24
      • 2019-02-15
      • 2011-12-12
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多