以下是方法:
package com.abstractions.authentication;
import java.net.*;
import java.util.Scanner;
public class ReadHtml {
private static URL u;
private static Scanner s;
public static String html;
public static String getUrl(String newUrl) throws Exception
{
u = new URL(newUrl);
s = new Scanner(u.openStream());
while (s.hasNext())
{
String line = s.nextLine();
try
{
StringBuilder sb = new StringBuilder();
while (s.hasNext())
{
sb.append(line);
sb.append("\n");
line = s.nextLine();
}
sb.toString();
html = sb.toString();
}
finally
{
}
}
return html;
}
}
以下是测试:
@Test
public void getHtmlOfUrl() throws Exception
{
WebDriver browser = new FirefoxDriver();
browser.navigate().to("https://crimestoppers-uk.org/give-information/give-information-online/");
Thread.sleep(3000);
String inputHtml = ReadHtml.getUrl("http://www.testforce.co.uk");
browser.findElement(By.id("tx_desc")).sendKeys(inputHtml);
}
问题:字符串值似乎保持为null,因此不会在文本字段中输入任何内容。