【问题标题】:Get a html site "input" element by python selenium通过 python selenium 获取 html 站点“输入”元素
【发布时间】:2020-02-10 19:16:44
【问题描述】:

我真的被困在这里了。 我使用 Python + Selenium 来自动填写网站表单。 所以我给网页提供了一些数据,然后“点击”一个按钮,之后一个新值出现在一个元素中,我想得到那个值,但我卡住了。

我应该如何将该值放入变量中? 我尝试使用适用于“click”的 find_element_by_xpath,适用于“send.keys”,但要从这里获取任何值,什么都没有。

请帮帮我!

附上网页检查的图片。

1

'''蟒蛇

from selenium import webdriver

browser = webdriver.Chrome('chromedriver.exe')

browser.get('https://...')

browser.find_element_by_xpath('//input[parameter- 
name="moduleId"]').send_keys('1234')

'''

所以到那时一切都很好。我可以用 1234 值填充“moduleId”元素。

但从这里我无法读回它。 所以如果我这样尝试:

'''蟒蛇

moduleId = browser.find_element_by_xpath('//input[@parameter-name="moduleId"]')

'''

输出什么都没有。

这是网站的 HTML 部分,有趣的是。

   html
   <input type="text" name="parameterValue" class="form-control" 
   placeholder="Value" spellcheck="false" autocomplete="off" data-bind="value:
   value, valueUpdate: 'keyup', autocomplete: { options: options, filtered: 
   true }, attr: { 'parameter-name': name, type: inputType }" parameter-name="moduleId">

【问题讨论】:

  • 由于您没有显示任何代码,我只需要猜测您缺少 elemenet.get_attribute("value")(或任何 Python 等价物)。
  • 请发布 html 示例,并显示您到目前为止编码的内容/您遇到的问题。 Stackoverflow 不是为您编写代码,而是帮助您编写代码。 stackoverflow.com/help/how-to-ask

标签: python selenium selenium-chromedriver


【解决方案1】:

使用这个来获取输入元素的值:

input.get_attribute('value')

您也可以一次性完成所有操作,例如

browser.find_element_by_xpath('//input[@parameter-name="moduleId"]').get_attribute('value')

【讨论】:

  • 感谢@scilence 的帮助。不幸的是我有这个错误信息:
  • 文件“C:/Users/zolag/Desktop/Legrand/scraper.py”,第 38 行,在
  • moduleId_value = input.get_attribute('value')
  • AttributeError: 'builtin_function_or_method' 对象没有属性 'get_attribute'
  • 您很可能希望使用moduleId_value = moduleId.get_attribute('value') 您需要指定从何处获取属性“值”。 'input' 不是预定义的方法或变量。
【解决方案2】:

Thx4 @scilence

我做了这个修改:

moduleId = browser.find_element_by_xpath('//input[@parameter-name="moduleId"]')
moduleId_value = moduleId.get_attribute('value')
print("data: ",moduleId_value)

输出就是我需要的!

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多