通过python硒获取html站点的“ input”元素-已解决

问题描述 投票:-2回答:2

我真的被困在这里。我使用Python + Selenium来自动化网站表单填充。因此,我将一些数据输入到网页中,然后单击按钮,然后在一个元素中出现一个新值,我想获取该值,但我坚持了。

我应该如何将该值放入变量中?我试图使用find_element_by_xpath来实现“点击”,而对于“ send.keys”来实现,但是从这里获取任何值,什么都没有。

请帮助我!

附上网页检查的图片。

1

'''python

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”元素。

但是从这里我不能读回来。所以如果我这样尝试:

'''python

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">
python selenium selenium-chromedriver
2个回答
1
投票

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

input.get_attribute('value')

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

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

0
投票

Thx4 @scilence

我进行了此修改:

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

输出就是我所需要的!

© www.soinside.com 2019 - 2024. All rights reserved.