2015-04-18 28 views
2

编辑: 为了让这件事清楚,什么即时试图做到这一点使程序去http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS并点击红色的“进入并投票”按钮的HtmlUnit点击一个链接按钮 - Java的

我“M试图做的是编程方式访问一个网页,点击href按钮是这样的:

<a href="http://www.ultimateprivateservers.com/index.php?a=in&amp;u=IkovPS&amp;sid=cSnJc3vgjV1P8rOe3l88Dv5ut1Wx1aBU" class="btn btn-danger">Enter and vote</a> 

我已经看了有htmlUnit几个TUTS,我似乎无法得到这个工作。我究竟做错了什么?有人能指出我正确的方向吗?我对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

尝试使用page.getElementbyID(“在这里指定的div ID不是类名“)。在你的代码中,你的做法完全相反。 – rakeeee

+0

@rakeeee该按钮没有div ID,div放入的按钮包含2个按钮:s –

回答

2

我看了一下页面,能够用稍微不同的方法投票。我更喜欢使用Selenium(http://www.seleniumhq.org/download/)。我能够使用Java中的Selenium使用下面非常粗糙的代码成功投票。您可以编辑和优化此代码以满足您的特定需求。我在Internet Explorer驱动程序中观看了整个过程,但如果您不想显示窗口,也可以使用PhantomJS(http://phantomjs.org/download.html)作为驱动程序。这是我简单的代码,setProperty方法的第二个参数是路径到驱动程序可执行文件,这将是唯一的计算机(您也可以下载IE驱动Selenium的下载页面上,以及):

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"))); 
+0

它的作用像魅力!非常感谢帮助,谢谢:) –