gpt4 book ai didi

java - htmlUnit 单击链接按钮 - Java

转载 作者:行者123 更新时间:2023-11-30 08:53:42 27 4
gpt4 key购买 nike

编辑:为了使整个事情清楚,我试图做的是让程序转到http://www.ultimateprivateservers.com/index.php?a=in&u=IkovPS。然后点击红色的“进入并投票”按钮

我想做的是以编程方式访问网页并单击如下所示的 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)

非常感谢任何帮助。

最佳答案

我查看了该页面,并能够使用略有不同的方法进行投票。我更喜欢使用 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")));

关于java - htmlUnit 单击链接按钮 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29712905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com