Solución:
En una palabra, no. No es posible usar la API de Selenium WebDriver. Esto se ha discutido hasta el cansancio en el seguimiento de problemas del proyecto y la función no se agregará a la API.
Es posible obtener el código de respuesta de una solicitud http usando Selenium y Chrome o Firefox. Todo lo que tiene que hacer es iniciar Chrome o Firefox en modo de registro. Les mostraré algunos ejemplos a continuación.
java + selenio + cromo
Aquí hay un ejemplo de java + Selenium + Chrome, pero supongo que se puede hacer en cualquier idioma (python, c #, …).
Todo lo que necesita hacer es decirle a chromedriver que haga “Network.enable”. Esto se puede hacer habilitando el registro de rendimiento.
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
Una vez realizada la solicitud, todo lo que tiene que hacer es obtener e iterar los registros de rendimiento y encontrar “Network.responseReceived” para la URL solicitada:
LogEntries logs = driver.manage().logs().get("performance");
Aquí está el código:
import java.util.Iterator;
import java.util.logging.Level;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class TestResponseCode
public static void main(String[] args)
// simple page (without many resources so that the output is
// easy to understand
String url = "http://www.york.ac.uk/teaching/cws/wws/webpage1.html";
DownloadPage(url);
private static void DownloadPage(String url)
ChromeDriver driver = null;
try
ChromeOptions options = new ChromeOptions();
// add whatever extensions you need
// for example I needed one of adding proxy, and one for blocking
// images
// options.addExtensions(new File(file, "proxy.zip"));
// options.addExtensions(new File("extensions",
// "Block-image_v1.1.crx"));
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
// set performance logger
// this sends Network.enable to chromedriver
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
driver = new ChromeDriver(cap);
// navigate to the page
System.out.println("Navigate to " + url);
driver.navigate().to(url);
// and capture the last recorded url (it may be a redirect, or the
// original url)
String currentURL = driver.getCurrentUrl();
// then ask for all the performance logs from this request
// one of them will contain the Network.responseReceived method
// and we shall find the "last recorded url" response
LogEntries logs = driver.manage().logs().get("performance");
int status = -1;
System.out.println("nList of log entries:n");
for (Iterator it = logs.iterator(); it.hasNext();)
LogEntry entry = it.next();
try
JSONObject json = new JSONObject(entry.getMessage());
System.out.println(json.toString());
JSONObject message = json.getJSONObject("message");
String method = message.getString("method");
if (method != null
&& "Network.responseReceived".equals(method))
JSONObject params = message.getJSONObject("params");
JSONObject response = params.getJSONObject("response");
String messageUrl = response.getString("url");
if (currentURL.equals(messageUrl))
status = response.getInt("status");
System.out.println(
"---------- bingo !!!!!!!!!!!!!! returned response for "
+ messageUrl + ": " + status);
System.out.println(
"---------- bingo !!!!!!!!!!!!!! headers: "
+ response.get("headers"));
catch (JSONException e)
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("nstatus code: " + status);
finally
if (driver != null)
driver.quit();
La salida se ve así:
Navigate to http://www.york.ac.uk/teaching/cws/wws/webpage1.html
List of log entries:
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.frameAttached","params":"parentFrameId":"172.1","frameId":"172.2"
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.frameStartedLoading","params":"frameId":"172.2"
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.frameNavigated","params":"frame":"securityOrigin":"://","loaderId":"172.1","name":"chromedriver dummy frame","id":"172.2","mimeType":"text/html","parentId":"172.1","url":"about:blank"
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.frameStoppedLoading","params":"frameId":"172.2"
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.frameStartedLoading","params":"frameId":"3928.1"
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Network.requestWillBeSent","params":"request":"headers":"Upgrade-Insecure-Requests":"1","User-Agent":"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36","initialPriority":"VeryHigh","method":"GET","mixedContentType":"none","url":"http://www.york.ac.uk/teaching/cws/wws/webpage1.html","frameId":"3928.1","requestId":"3928.1","documentURL":"http://www.york.ac.uk/teaching/cws/wws/webpage1.html","initiator":"type":"other","loaderId":"3928.1","wallTime":1.47619492749007E9,"type":"Document","timestamp":20226.652971
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Network.responseReceived","params":"frameId":"3928.1","requestId":"3928.1","response":"headers":"Accept-Ranges":"bytes","Keep-Alive":"timeout=4, max=100","Cache-Control":"max-age=300","Server":"Apache/2.2.22 (Ubuntu)","Connection":"Keep-Alive","Content-Encoding":"gzip","Vary":"Accept-Encoding","Expires":"Tue, 11 Oct 2016 14:13:47 GMT","Content-Length":"1957","Date":"Tue, 11 Oct 2016 14:08:47 GMT","Content-Type":"text/html","connectionReused":false,"timing":"pushEnd":0,"workerStart":-1,"proxyEnd":-1,"workerReady":-1,"sslEnd":-1,"pushStart":0,"requestTime":20226.65335,"sslStart":-1,"dnsStart":0,"sendEnd":31.6569999995409,"connectEnd":31.4990000006219,"connectStart":0,"sendStart":31.5860000009707,"dnsEnd":0,"receiveHeadersEnd":115.645999998378,"proxyStart":-1,"encodedDataLength":-1,"remotePort":80,"mimeType":"text/html","headersText":"HTTP/1.1 200 OKrnDate: Tue, 11 Oct 2016 14:08:47 GMTrnServer: Apache/2.2.22 (Ubuntu)rnAccept-Ranges: bytesrnCache-Control: max-age=300rnExpires: Tue, 11 Oct 2016 14:13:47 GMTrnVary: Accept-EncodingrnContent-Encoding: gziprnContent-Length: 1957rnKeep-Alive: timeout=4, max=100rnConnection: Keep-AlivernContent-Type: text/htmlrnrn","securityState":"neutral","requestHeadersText":"GET /teaching/cws/wws/webpage1.html HTTP/1.1rnHost: www.york.ac.ukrnConnection: keep-alivernUpgrade-Insecure-Requests: 1rnUser-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36rnAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8rnAccept-Encoding: gzip, deflate, sdchrnAccept-Language: en-GB,en-US;q=0.8,en;q=0.6rnrn","url":"http://www.york.ac.uk/teaching/cws/wws/webpage1.html","protocol":"http/1.1","fromDiskCache":false,"fromServiceWorker":false,"requestHeaders":"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Upgrade-Insecure-Requests":"1","Connection":"keep-alive","User-Agent":"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36","Host":"www.york.ac.uk","Accept-Encoding":"gzip, deflate, sdch","Accept-Language":"en-GB,en-US;q=0.8,en;q=0.6","remoteIPAddress":"144.32.128.84","statusText":"OK","connectionId":11,"status":200,"loaderId":"3928.1","type":"Document","timestamp":20226.770012
---------- bingo !!!!!!!!!!!!!! returned response for http://www.york.ac.uk/teaching/cws/wws/webpage1.html: 200
---------- bingo !!!!!!!!!!!!!! headers: "Accept-Ranges":"bytes","Keep-Alive":"timeout=4, max=100","Cache-Control":"max-age=300","Server":"Apache/2.2.22 (Ubuntu)","Connection":"Keep-Alive","Content-Encoding":"gzip","Vary":"Accept-Encoding","Expires":"Tue, 11 Oct 2016 14:13:47 GMT","Content-Length":"1957","Date":"Tue, 11 Oct 2016 14:08:47 GMT","Content-Type":"text/html"
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Network.dataReceived","params":"dataLength":2111,"requestId":"3928.1","encodedDataLength":1460,"timestamp":20226.770425
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.frameNavigated","params":"frame":"securityOrigin":"http://www.york.ac.uk","loaderId":"3928.1","id":"3928.1","mimeType":"text/html","url":"http://www.york.ac.uk/teaching/cws/wws/webpage1.html"
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Network.dataReceived","params":"dataLength":1943,"requestId":"3928.1","encodedDataLength":825,"timestamp":20226.782673
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Network.loadingFinished","params":"requestId":"3928.1","encodedDataLength":2285,"timestamp":20226.770199
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.loadEventFired","params":"timestamp":20226.799391
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.frameStoppedLoading","params":"frameId":"3928.1"
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Page.domContentEventFired","params":"timestamp":20226.845769
"webview":"3b8eaedb-bd0f-4baa-938d-4aee4039abfe","message":"method":"Network.requestWillBeSent","params":"request":"headers":"Referer":"http://www.york.ac.uk/teaching/cws/wws/webpage1.html","User-Agent":"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36","initialPriority":"High","method":"GET","mixedContentType":"none","url":"http://www.york.ac.uk/favicon.ico","frameId":"3928.1","requestId":"3928.2","documentURL":"http://www.york.ac.uk/teaching/cws/wws/webpage1.html","initiator":"type":"other","loaderId":"3928.1","wallTime":1.47619492768527E9,"type":"Other","timestamp":20226.848174
status code: 200
java + Selenium + Firefox
Finalmente también encontré el truco para Firefox. Necesitas iniciar Firefox usando MOZ_LOG
y MOZ_LOG_FILE
variables de entorno y registrar solicitudes http a nivel de depuración (4 = PR_LOG_DEBUG) - map.put("MOZ_LOG", "timestamp,sync,nsHttp:4")
. Guarde el registro en un archivo temporal. Después de eso, obtenga el contenido del archivo de registro guardado y analícelo para el código de respuesta (usando algunas expresiones regulares simples). Primero detecta el inicio de la solicitud, identificando su id. (nsHttpChannel::BeginConnect [this=000000CED8094000])
, luego, en el segundo paso, busque el código de respuesta para ese ID de solicitud (nsHttpChannel::ProcessResponse [this=000000CED8094000 httpStatus=200])
.
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
public class TestFirefoxResponse
public static void main(String[] args)
throws InterruptedException, IOException
GeckoDriverService service = null;
// tell firefox to log http requests
// at level 4 = PR_LOG_DEBUG: debug messages, notices
// you could log everything at level 5, but the log file will
// be larger.
// create a temporary log file that will be parsed for
// response code
Map map = new HashMap();
map.put("MOZ_LOG", "timestamp,sync,nsHttp:4");
File tempFile = File.createTempFile("mozLog", ".txt");
map.put("MOZ_LOG_FILE", tempFile.getAbsolutePath());
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
service = builder.usingAnyFreePort()
.withEnvironment(map)
.build();
service.start();
WebDriver driver = new FirefoxDriver(service);
// test 200
String url = "https://api.ipify.org/?format=text";
// test 404
// String url = "https://www.advancedwebranking.com/lsdkjflksdjfldksfj";
driver.get(url);
driver.quit();
String logContent = FileUtils.readFileToString(tempFile);
ParseLog(logContent, url);
private static void ParseLog(String logContent, String url) throws MalformedURLException
Pattern.DOTALL);
m = p.matcher(logContent);
if (m.find())
String responseCode = m.group(1);
System.out.println("response code found " + responseCode);
else
System.out.println("response code not found");
La salida para esto será
id de solicitud = 0000007653D67000 código de respuesta encontrado 200
Los encabezados de respuesta también se pueden encontrar en el archivo de registro. Puedes conseguirlos si quieres.
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp http response [
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp HTTP/1.1 404 Not Found
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Accept-Ranges: bytes
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Cache-control: no-cache="set-cookie"
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Content-Type: text/html; charset=utf-8
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Date: Thu, 02 Nov 2017 14:54:36 GMT
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp ETag: "7969-55bc076a61e80"
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Last-Modified: Tue, 17 Oct 2017 16:17:46 GMT
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Server: Apache/2.4.23 (Amazon) PHP/5.6.24
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Set-Cookie: AWSELB=5F256FFA816C8E72E13AE0B12A17A3D540582F804C87C5FEE323AF3C9B638FD6260FF473FF64E44926DD26221AAD2E9727FD739483E7E4C31784C7A495796B416146EE83;PATH=/
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Content-Length: 31081
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Connection: keep-alive
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp OriginalHeaders
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Accept-Ranges: bytes
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Cache-control: no-cache="set-cookie"
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Content-Type: text/html; charset=utf-8
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Date: Thu, 02 Nov 2017 14:54:36 GMT
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp ETag: "7969-55bc076a61e80"
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Last-Modified: Tue, 17 Oct 2017 16:17:46 GMT
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Server: Apache/2.4.23 (Amazon) PHP/5.6.24
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Set-Cookie: AWSELB=5F256FFA816C8E72E13AE0B12A17A3D540582F804C87C5FEE323AF3C9B638FD6260FF473FF64E44926DD26221AAD2E9727FD739483E7E4C31784C7A495796B416146EE83;PATH=/
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Content-Length: 31081
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp Connection: keep-alive
2017-11-02 14:54:36.775000 UTC - [Socket Thread]: I/nsHttp ]
2017-11-02 14:54:36.775000 UTC - [Main Thread]: D/nsHttp nsHttpChannel::OnStartRequest [this=0000008A65D85000 request=0000008A65D1F900 status=0]
2017-11-02 14:54:36.775000 UTC - [Main Thread]: D/nsHttp nsHttpChannel::ProcessResponse [this=0000008A65D85000 httpStatus=404]
Para aquellas personas que usan Python, podrían considerar Selenium Wire, una biblioteca para inspeccionar las solicitudes realizadas por el navegador durante una prueba.
Obtiene acceso a las solicitudes a través del driver.requests
atributo:
from seleniumwire import webdriver # Import from seleniumwire
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
# Go to the Google home page
driver.get('https://www.google.com')
# Access requests via the `requests` attribute
for request in driver.requests:
if request.response:
print(
request.url,
request.response.status_code,
request.response.headers['Content-Type']
)
Huellas dactilares:
https://www.google.com/ 200 text/html; charset=UTF-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png 200 image/png
https://consent.google.com/status?continue=https://www.google.com&pc=s×tamp=1531511954&gl=GB 204 text/html; charset=utf-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png 200 image/png
https://ssl.gstatic.com/gb/images/i2_2ec824b0.png 200 image/png
https://www.google.com/gen_204?s=webaft&t=aft&atyp=csi&ei=kgRJW7DBONKTlwTK77wQ&rt=wsrt.366,aft.58,prt.58 204 text/html; charset=UTF-8
...
La biblioteca le brinda la posibilidad de acceder a encabezados, código de estado, contenido del cuerpo, así como la capacidad de modificar encabezados y reescribir URL.
Al final de la artículo puedes encontrar los comentarios de otros sys admins, tú incluso eres capaz dejar el tuyo si dominas el tema.