如何在量角器中获取页面的来源

时间:2014-05-20 22:38:14

标签: selenium-webdriver protractor

我正在使用Protractor来运行我的测试,并希望捕获整页的源代码以进行调试。使用Selenium WebDriver

如何获取页面源(手动查看时类似于View / Source的内容)

3 个答案:

答案 0 :(得分:6)

我不确定您是否在寻找当前由驱动程序打开的页面的源代码,如果是,您可以使用以下方法获取源代码。

System.out.println(driver.getPageSource());
希望它有所帮助。

答案 1 :(得分:4)

要获取网页的来源,Protractor documentation表示要使用getPageSource()方法。记住它会返回一个Promise。

browser.get('http://www.example.com')
.then( function () {
    return browser.getPageSource();
.then( function (txt) {
    console.log(txt);
})

答案 2 :(得分:0)

试试这个:

 <script>
    $("#addAnother").click(function () {
        $.get('ServiceOrderLineEntryRow', function (template) {
            $("#ServiceOrderLineEditor").append(template);
            var searchedText = "UNIT_PRICE"; //Search for some text in the list item to identify the HTML that contains the GUID
            $("li:contains('" + searchedText + "')").each(function (i, element) {
                var str = $(element).html(); //The HTML of the GUID based fields
                var svcName = "";
                var svcID = "";
                var unitPrice = "";
                var taxable = "";
                $(str).each(function (j, el) {
                    var tag = $(el).attr("id");//Set the ID tag names e.g.  id="SOD_c1567fa3-b9da-4a76-a72f-6f2df36be4d8__SERVICE_TYPE_NAME"
                    if (tag !== undefined)
                    {
                        if (tag.endsWith("SERVICE_TYPE_NAME")) {
                            svcName = tag;
                        }
                        if (tag.endsWith("SERVICE_TYPE_ID")) {
                            svcID = tag;
                        }
                        if (tag.endsWith("UNIT_PRICE")) {
                            unitPrice = tag;
                        }
                        if (tag.endsWith("TAXABLE")) {
                            taxable = tag;
                        }
                    }
                });

                $(document).ready(function () {
                $("#"+svcName).autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '@Url.Action("GetServiceList", "AccountServiceOrder")',
                        datatype: "json",
                        data: {
                            term: request.term
                        }, //Get the list of Services that narrow down as you type into the Service field
                        success: function (data) {      
                            response($.map(data, function (val, item) {
                                return {
                                    label: val.NAME,
                                    value: val.NAME,
                                    serviceID: val.SERVICE_TYPE_ID,
                                    unitPrice: val.COST,
                                    taxable: val.TAXABLE //Map the results of the service query to the response
                                }
                            }))
                        },
                        error: function (error) {
                            alert(error);
                        } 
                    })
                },
                select: function (event, ui) {
                    var item = ui.item;
                    if (item) {
                        $("#" + svcID).val(item.serviceID);
                        $("#" + unitPrice).val(item.unitPrice.toFixed(2));
                        $("#" + taxable).prop("checked",taxable);  //Set the GUID based text fields to the query results
                    }
                }
                });
            });
            });
        });
    });
</script>