以下是我使用的代码,
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Driver;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class VerifyLinks {
//@SuppressWarnings("deprecation")
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\sselvaraj\\Documents\\Java\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/#q=selenium");
List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Total links are "+links.size());
for(int i=0;i<links.size();i++)
{
WebElement ele= links.get(i);
String url=ele.getAttribute("href");
verifyLinkActive(url);
}
}
public static void verifyLinkActive(String linkUrl)
{
try
{
URL url = new URL(linkUrl);
HttpURLConnection httpURLConnect=HttpURLConnection)
url.openConnection();
httpURLConnect.setConnectTimeout(3000);
httpURLConnect.connect();
if(httpURLConnect.getResponseCode()==200)
{
System.out.println(linkUrl + " - "+httpURLConnect.getResponseMessage());
}
if(httpURLConnect.getResponseCode()==HttpURLConnection.HTTP_NOT_FOUND)
{
System.out.println(linkUrl + " - "
+httpURLConnect.getResponseMessage() + " - "
+HttpURLConnection.HTTP_NOT_FOUND);
}
} catch (Exception e) {
}
}
}
以下是我收到的回复,
Sep 20, 2016 10:19:13 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
1474384754549 Marionette INFO Listening on port 53039
Sep 20, 2016 10:19:18 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
**Total links are 0**
您能帮忙解决这个问题吗?感谢您的帮助!!
答案 0 :(得分:0)
它正在我的机器上工作。我没有使用geckodriver,因为我的firefox版本是43.错误可能是由于geckodriver。
答案 1 :(得分:0)
对于铸造,不包括开括号
HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
现在可以用了!!