修改 为了使整个事情清楚,我试图做的是让程序转到http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS并点击红色的“输入并投票”按钮
我要做的是以编程方式访问网页,然后点击如下所示的href
按钮:
<a href="http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS&sid=cSnJc3vgjV1P8rOe3l88Dv5ut1Wx1aBU" class="btn btn-danger">Enter and vote</a>
我看了htmlUnit
的一些内容,我似乎无法让这个工作。我究竟做错了什么?有人能指出我正确的方向吗?我对java不是很好,所以会让人感到困惑。
这是我的代码:
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;
public class HtmlUnitFormExample {
public static void main(String[] args) throws Exception {
WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS");
HtmlLink enterAndVoteButton =
page.getElementByName("btn btn-danger");
page=enterAndVoteButton.click();
HtmlDivision resultStatsDiv =
page.getFirstByXPath("//div[@id='vote_message_fail']");
System.out.println(resultStatsDiv.asText());
webClient.closeAllWindows();
}
}
这是控制台日志:
SEVERE: IOException when getting content for iframe: url=[http://a.tribalfusion.com/p.media/aPmQ0x0qPp4WYBPGZbE4PJZdodZanVdfb0bQjYrBeXaisRUvDUFB5WHn0mFBoRU7y1T3s5TUj2qfXmEjIYbYgUHBUoP7Cns7uptfG5Evl5teN5ABLpbbL0V7R1VF3XGjNmqJQ3FQ2WFJBW6Q2QEf1ScUMQdUOYtbuTPbx2G32XrnZcVmun4PQgQmnH4HQrXHBAMTAJplZd1Wp/3002246/adTag.html]
org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:188)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:178)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1313)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1230)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:338)
at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPageIfPossible(BaseFrameElement.java:184)
at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPage(BaseFrameElement.java:122)
at com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames(HtmlPage.java:1993)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:238)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:475)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:342)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:407)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:392)
at HtmlUnitFormExample.main(HtmlUnitFormExample.java:7)
Caused by: org.apache.http.HttpException: Unsupported Content-Coding: none
at org.apache.http.client.protocol.ResponseContentEncoding.process(ResponseContentEncoding.java:98)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:139)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:200)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
... 14 more
Apr 18, 2015 5:28:37 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[*] attributeName=[name] attributeValue=[btn btn-danger]
at com.gargoylesoftware.htmlunit.html.HtmlPage.getElementByName(HtmlPage.java:1747)
at HtmlUnitFormExample.main(HtmlUnitFormExample.java:10)
非常感谢任何帮助。
答案 0 :(得分:2)
我看了一下页面,并且能够使用稍微不同的方法进行投票。我更喜欢使用Selenium(http://www.seleniumhq.org/download/)。我能够使用Java中的Selenium成功地使用下面的粗略代码进行投票。您可以根据自己的特定需求编辑和优化此代码。我在Internet Explorer驱动程序中查看了整个过程,但如果您不希望窗口显示,也可以使用PhantomJS(http://phantomjs.org/download.html)作为驱动程序。这是我的简单代码,setProperty方法的第二个参数是驱动程序可执行文件的路径,这对您的计算机来说是唯一的(您也可以在Selenium下载页面上下载IE驱动程序):
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
public class SeleniumTest()
{
public static void main(String[] args)
{
try
{
System.setProperty("webdriver.ie.driver"," IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS");
Thread.sleep(3000); //use the wait as shown below
WebElement button = driver.findElement(By.linkText("Enter and vote"));
button.click();
driver.close();
driver.quit();
}catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
等待页面加载的更好方法是这样的:
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Enter and vote")));
你也可以使用类似的按钮找到按钮:
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("btn-danger")));