Selenium 2: Detect content type of link destinations

后端 未结 3 1960
猫巷女王i
猫巷女王i 2021-01-15 19:16

I am using the Selenium 2 Java API to interact with web pages. My question is: How can i detect the content type of link destinations?

Basically, this is the backgro

相关标签:
3条回答
  • 2021-01-15 19:51

    As Jochen suggests, the way to get the Content-type without also downloading the content is HTTP HEAD, and the selenium webdrivers does not seem to offer functionality like that. You'll have to find another library to help you with fetching the content type of an url.

    A Java library that can do this is Apache HttpComponents, especially HttpClient.

    (The following code is untested)

    HttpClient httpclient = new DefaultHttpClient();
    HttpHead httphead = new HttpHead("http://foo/bar");
    HttpResponse response = httpclient.execute(httphead);
    BasicHeader contenttypeheader = response.getFirstHeader("Content-Type");
    
    System.out.println(contenttypeheader);
    

    The project publishes JavaDoc for HttpClient, the documentation for the HttpClient interface contains a nice example.

    0 讨论(0)
  • 2021-01-15 19:53

    You can figure out the content type will processing the data coming in. Not sure why you need to figure this out first. If so, use the HEAD method and look at the Content-Type header.

    0 讨论(0)
  • 2021-01-15 20:08

    You can retrieve all the URLs from the DOM, and then parse the last few characters of each URL (using a java regex) to determine the link type.

    You can parse characters proceeding the last dot. For example, in the url http://yoursite.com/whatever/test.pdf, extract the pdf, and enforce your test logic accordingly.

    Am I oversimplifying your problem?

    0 讨论(0)
提交回复
热议问题