在selenium webdriver(浏览器)中呈现HTTP响应(HTML内容)

时间:2016-04-22 05:30:43

标签: python google-chrome selenium selenium-webdriver google-chrome-devtools

我正在使用Requests模块向网站发送GETPOST个请求,然后处理他们的回复。如果Response.text符合某个条件,我希望在浏览器中打开它。目前我正在使用selenium包,并通过selenium webdriver将请求重新发送到网页。但是,由于我已经获得了一次响应,我觉得它效率低下,所以有没有办法将这个获得的Response对象直接渲染到通过selenium打开的浏览器中?

修改 我能想到的一种hacky方式是将response.text写入临时文件并在浏览器中打开它。如果有比这更好的方法,请告诉我吗?

1 个答案:

答案 0 :(得分:7)

要使用Selenium直接呈现某些HTML,您可以将数据方案与get方法一起使用:

<h4>Create a new account.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })

<div class="form-group">
    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.MobileNo, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.MobileNo, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Address, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Address, new { @class = "form-control" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Register" />
    </div>
</div>

或者您可以使用一段脚本编写页面:

from selenium import webdriver
import requests

content = requests.get("http://stackoverflow.com/").content

driver = webdriver.Chrome()
driver.get("data:text/html;charset=utf-8," + content)