无法使用 HtmlUnit 单击 youtube AJAX 链接
我正在尝试使用 htmlunit 单击 youtube 中的链接。这是代码:
HtmlPage page = webClient.getPage("http://www.youtube.com");
HtmlAnchor link = page.getFirstByXPath("//a[contains(@href, 'accounts/ServiceLogin?')]");
page = link.click();
HtmlForm form = page.getFirstByXPath("//form[@id='gaia_loginform']");
form.getInputByName("Email").setValueAttribute("login");
form.getInputByName("Passwd").setValueAttribute("pass");
page = form.getInputByName("signIn").click();
page = webClient.getPage("http://www.youtube.com/watch?v=_cLaKr1j55w");
page.getElementById("watch-tab-favorite").click();
但它不起作用:(我试图单击的元素的代码是
<div id="watch-tab-favorite" class="watch-tab" onclick="if (!_hasclass(this, 'disabled')) { yt.www.watch.favorites.add('addToFavesForm'); }">
我也尝试过 page.executeJavaScript("yt.www.watch.favorites.add('addToFavesForm' );");
也没有成功。似乎 htmlunit 根本没有发送请求。我该如何修复它?TIA
I'm trying to use htmlunit to click on a link in youtube. Here's the code:
HtmlPage page = webClient.getPage("http://www.youtube.com");
HtmlAnchor link = page.getFirstByXPath("//a[contains(@href, 'accounts/ServiceLogin?')]");
page = link.click();
HtmlForm form = page.getFirstByXPath("//form[@id='gaia_loginform']");
form.getInputByName("Email").setValueAttribute("login");
form.getInputByName("Passwd").setValueAttribute("pass");
page = form.getInputByName("signIn").click();
page = webClient.getPage("http://www.youtube.com/watch?v=_cLaKr1j55w");
page.getElementById("watch-tab-favorite").click();
but it doesn't work :( The code of the element I'm trying to click is
<div id="watch-tab-favorite" class="watch-tab" onclick="if (!_hasclass(this, 'disabled')) { yt.www.watch.favorites.add('addToFavesForm'); }">
I've also tried page.executeJavaScript("yt.www.watch.favorites.add('addToFavesForm');");
with no success either. Seems like htmlunit isn't sending the request at all. How can I fix it? TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看http://htmlunit.sourceforge.net/faq.html#AJAXDoesNotWork
否则,请使用 HtmlUnit 用户列表
Have a look at http://htmlunit.sourceforge.net/faq.html#AJAXDoesNotWork
Otherwise, please use HtmlUnit user list