如何在不使用vb​​script打开浏览器中的每个链接的情况下检查页面上断开的链接

时间:2018-12-01 16:01:59

标签: https vbscript hp-uft broken-links

请使用VBScript代码帮助我,以自动识别页面上的断开链接和图像(我正在使用HP UFT工具)。当我在互联网上搜索时,发现很少的命名空间,例如“ WinHttp.WinHttpRequest.5.1”,MSXML2.ServerXMLHTTP 但是对于每个链接,这些人总是向我返回200的状态

下面是我正在运行的代码:

Set LinkDesObj = Description.Create
LinkDesObj("micclass").value="Link"
LinkDesObj("url").value = "https://testCRM.azuresites.com" 
Set LinkCollection = Browser("TestCRM").Page("TestCRM").ChildObjects(LinkDesObj)

For i = 0 To LinkCollection.count-1 Step 1

If LinkCollection(i).GetROProperty("visible")=True Then
    LinkCollection(i).highlight
    URL = LinkCollection(i).GetROProperty("url")
    status = VerifyTheURL(URL)
End If

  Next


  Function VerifyTheURL(URL)

   Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
   objWinHTTP.Option(WinHttpRequestOption_EnableRedirects)=False
   objWinHTTP.Open "GET", URL, False
   objWinHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"

    'Send the Request to the Server and capture the response
   objWinHTTP.Send
   objWinHTTP.WaitForResponse(10)
   iReturnVal = objWinHTTP.Status

   'Find out if the Link exists or is broken
    If iReturnVal = 200 Then
        msgbox "Link - " & URL & " Exists"
    ElseIf iReturnVal = 404 Then
         msgbox "Link - " & URL & " is Broken"
     Else
     msgbox "Code" - iReturnVal
     End If

     Set objWinHTTP = Nothing

      End Function

请让我知道上面的代码对于HTTPS URL是否正确?

1 个答案:

答案 0 :(得分:0)

200表示成功。 100表示​​它仍在继续。 200s表示全部或部分成功。 300多半是关于信息的。 Web服务器说400s不能执行您要求的任何操作。 500s是服务器错误,就像它崩溃了一样。

对于Windows的错误号,请添加12,000。 IE 12404的状态为404

也考虑使用用户Head而不是Get

HEAD方法

  

HEAD与GET几乎相同,但是没有响应主体。

     

换句话说,如果GET / users返回用户列表,则HEAD   / users将发出相同的请求,但不会返回以下列表   用户。

     

HEAD请求对于检查GET请求将返回什么很有用   在实际发出GET请求之前-例如在下载大型   文件或响应正文。

这是来自WinInet.h

// HTTP Response Status Codes:
//

#define HTTP_STATUS_CONTINUE            100 // OK to continue with request
#define HTTP_STATUS_SWITCH_PROTOCOLS    101 // server has switched protocols in upgrade header

#define HTTP_STATUS_OK                  200 // request completed
#define HTTP_STATUS_CREATED             201 // object created, reason = new URI
#define HTTP_STATUS_ACCEPTED            202 // async completion (TBS)
#define HTTP_STATUS_PARTIAL             203 // partial completion
#define HTTP_STATUS_NO_CONTENT          204 // no info to return
#define HTTP_STATUS_RESET_CONTENT       205 // request completed, but clear form
#define HTTP_STATUS_PARTIAL_CONTENT     206 // partial GET furfilled

#define HTTP_STATUS_AMBIGUOUS           300 // server couldn't decide what to return
#define HTTP_STATUS_MOVED               301 // object permanently moved
#define HTTP_STATUS_REDIRECT            302 // object temporarily moved
#define HTTP_STATUS_REDIRECT_METHOD     303 // redirection w/ new access method
#define HTTP_STATUS_NOT_MODIFIED        304 // if-modified-since was not modified
#define HTTP_STATUS_USE_PROXY           305 // redirection to proxy, location header specifies proxy to use
#define HTTP_STATUS_REDIRECT_KEEP_VERB  307 // HTTP/1.1: keep same verb

#define HTTP_STATUS_BAD_REQUEST         400 // invalid syntax
#define HTTP_STATUS_DENIED              401 // access denied
#define HTTP_STATUS_PAYMENT_REQ         402 // payment required
#define HTTP_STATUS_FORBIDDEN           403 // request forbidden
#define HTTP_STATUS_NOT_FOUND           404 // object not found
#define HTTP_STATUS_BAD_METHOD          405 // method is not allowed
#define HTTP_STATUS_NONE_ACCEPTABLE     406 // no response acceptable to client found
#define HTTP_STATUS_PROXY_AUTH_REQ      407 // proxy authentication required
#define HTTP_STATUS_REQUEST_TIMEOUT     408 // server timed out waiting for request
#define HTTP_STATUS_CONFLICT            409 // user should resubmit with more info
#define HTTP_STATUS_GONE                410 // the resource is no longer available
#define HTTP_STATUS_LENGTH_REQUIRED     411 // the server refused to accept request w/o a length
#define HTTP_STATUS_PRECOND_FAILED      412 // precondition given in request failed
#define HTTP_STATUS_REQUEST_TOO_LARGE   413 // request entity was too large
#define HTTP_STATUS_URI_TOO_LONG        414 // request URI too long
#define HTTP_STATUS_UNSUPPORTED_MEDIA   415 // unsupported media type
#define HTTP_STATUS_RETRY_WITH          449 // retry after doing the appropriate action.

#define HTTP_STATUS_SERVER_ERROR        500 // internal server error
#define HTTP_STATUS_NOT_SUPPORTED       501 // required not supported
#define HTTP_STATUS_BAD_GATEWAY         502 // error response received from gateway
#define HTTP_STATUS_SERVICE_UNAVAIL     503 // temporarily overloaded
#define HTTP_STATUS_GATEWAY_TIMEOUT     504 // timed out waiting for gateway
#define HTTP_STATUS_VERSION_NOT_SUP     505 // HTTP version not supported