Robobrowser。如何在没有名称属性的情况下填写输入

时间:2019-03-03 14:53:39

标签: python python-2.7

 <form class="form form_horizontal" novalidate="">
 <div class="form__field">
  <label class="form__label">
      <span class="tanker-text">получить ссылку на&nbsp;приложение по&nbsp;смс</span>. 
  </label>
  <div class="y-input y-input_theme_normal y-input_clearable y-input_size_m y-input_width_ form__input">
     <div class="y-input__box">
        ::before
        <span class="y-input__clear"></span>
 <input type="tel" value="" maxlength="20" placeholder="+7XXXXXXXXXX" cols="10" class="y-input__control">
  </div>
  </div>
  </div>
   <button type="submit" class="y-button y-button_theme_action y-button_size_l y-button_type_submit form__button" role="button" aria-haspopup="true">
 <span class="y-button__text"><span class="tanker-text">Получить</span></span>. 
  </button>
  <noscript></noscript>

尝试填写数字字段,但这不起作用

from robobrowser import RoboBrowser
import re


rb =  RoboBrowser(history=True)
rb.open('myurl')


ff = rb.get_form(class_ = 'form form_horizontal')
ff['y-input__control'].value = 'myphonenumber' 

如何找到并填写该字段?提交表单之后?

1 个答案:

答案 0 :(得分:0)

我使用以下代码进行了测试

html:

<form class="form form_horizontal" novalidate="" action="http://localhost:3000/test" method="POST">
    <div class="form__field">
        <label class="form__label">
            <span class="tanker-text">получить ссылку на&nbsp;приложение по&nbsp;смс</span>.
        </label>
        <div class="y-input y-input_theme_normal y-input_clearable y-input_size_m y-input_width_ form__input">
            <div class="y-input__box">
                ::before
                <span class="y-input__clear"></span>
                <input type="tel" value="" maxlength="20" placeholder="+7XXXXXXXXXX" cols="10" class="y-input__control">
            </div>
        </div>
    </div>
    <button type="submit" class="y-button y-button_theme_action y-button_size_l y-button_type_submit form__button"
        role="button" aria-haspopup="true">
        <span class="y-button__text"><span class="tanker-text">Получить</span></span>.
    </button>
</form>

节点:
-渲染HTML
-对于表单提交测试,已创建 POST API(http://localhost:3000/test

var express = require('express');
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())


app.use('/', express.static(__dirname + '/public'));
app.post('/test', (req, res) => {
    console.log('test POST', req.body, req.params);
    res.status(200).json({ status: true })
});

app.listen(3000, function () { console.log('listening') });

Python:

from robobrowser import RoboBrowser
import re

url = 'http://localhost:3000/test.html'
rb =  RoboBrowser()
rb.open(url)


ff = rb.get_form(class_=re.compile('form'))

# yInputControl = rb.find(class_=re.compile(r'\y-input__control\b'))
yInputControl = rb.find(class_='y-input__control')
print(yInputControl)
yInputControl.value = 'myphonenumber' 
rb.submit_form(ff)