HtmlUnit无法在youtube上完全加载页面
HtmlUnit doesnt fully load page on youtube
我的程序正在进入一个youtube视频链接,并试图获得评论框。我知道如何获取它,但当我尝试访问包含它的div时,它显示为正在加载的div,所以我假设页面没有完全加载。我尝试了这些解决方案,但都不起作用:
while(pagina.getFirstByXPath("//div[@id='comment-section-renderer']/div")
.toString().contains("loading")) {
synchronized(pagina) {
pagina.wait(2000);
}
}
另一种方式:
cliente.waitForBackgroundJavaScript(100000);
该页面从gmail登录加载,我在加载视频页面时检查了用户是否成功登录。
这是方法的代码
public HtmlPage comentaVideo(String correo, String pass, String video,
String comentario) throws ... {
String url= "https://www.youtube.com"+video;
HtmlPage pagina;
HtmlDivision division;
HtmlButton boton;
HtmlTextInput input;
pagina = cliente.getPage("https://www.youtube.com/watch?v=E2b9PiqobWg");
boton = pagina.getFirstByXPath("//div[@id='yt-masthead-signin']/div/button");
//press sign in button
pagina = boton.click();
pagina=iniciaSesion(correo,pass,pagina); //Login gmail (working)
System.out.println(pagina.getUrl().toString()); //just for debug
//Trying to get the coment box div
division = pagina.getFirstByXPath("//div[@id='comment-section-renderer']/div");
//verifying that the div is correct
System.out.println(division.toString());
//some tests...
pagina=division.click();
boton= pagina.getFirstByXPath("//div[@id='comment-simplebox']/div/button[2]");
pagina=boton.click();
return pagina;
}
现在我认识到了这个问题,这是更新后的方法,仍然不起作用。。。
public HtmlPage comentaVideo(String correo, String pass, String video, String comentario) throws FailingHttpStatusCodeException, MalformedURLException, IOException, ErrorSesionNoIniciada, InterruptedException{
String url= "https://www.youtube.com"+video;
HtmlPage pagina;
HtmlDivision division;
HtmlButton boton;
HtmlTextInput input;
pagina = cliente.getPage("https://www.youtube.com/watch?v=E2b9PiqobWg");
boton = pagina.getFirstByXPath("//div[@id='yt-masthead-signin']/div/button");
pagina = boton.click();
pagina=iniciaSesion(correo,pass,pagina);
System.out.println(pagina.getUrl().toString());
//Parte no funcional
division = pagina.getFirstByXPath("//div[@id='comment-section-renderer']/div");
boton = division.getFirstByXPath("//div[@id='comment-section-renderer']/div[2]/button"); //best comments button
while(boton == null){ //while this button is not loaded
ScriptResult sr=pagina.executeJavaScript("window.scrollBy(0,60000)");
cliente.waitForBackgroundJavaScript(1000);
pagina=(HtmlPage)sr.getNewPage();
boton = division.getFirstByXPath("//div[@id='comment-section-renderer']/div[2]/button");
}
System.out.println(boton.toString());
//just for testing
division = pagina.getFirstByXPath("//div[@id='comment-section-renderer']/div");
System.out.println(division.toString());
pagina=division.click();
boton= pagina.getFirstByXPath("//div[@id='comment-simplebox']/div/button[2]");
pagina=boton.click();
return pagina;
我还尝试将内部页面的高度设置为最大大小。(代码有未使用的var和throws,因为这只是为了测试,当我得到解决方案时,我会用最终版本更新它)
第1版:更改WHILE循环条件,仍然不工作
查看youtube页面结构,加载评论部分的ajax似乎只有在向下滚动页面时才会触发,直到它在页面中可见。您可能想先尝试模拟滚动,然后依赖于等待"加载"字符串从容器div的内部html中消失的循环。
还要考虑到,当他们推出更新时,这种行为可能很快就会改变。
编辑:
在使用chromeinspector进行检查后,似乎有更多的div元素包含"loading"(子)字符串,即使在通过ajax填充注释部分之后也是如此。我建议修改您的条件,使新的期望字符串出现,而不是"加载"。例如,您可以搜索"热门评论"(按钮文本)或"添加公共评论…"(评论发布文本区域的占位符)。
相关文章:
- HtmlUnit无法在youtube上完全加载页面
- 如何在PHP数组中加载下一个youtube
- 借助JS/Jquery函数,通过其URL加载youtube视频
- 在 Meteor 中加载 youtube 播放器 API
- 在 iframe 中加载 Youtube Player API
- 通过带有身份验证的YQL加载youtube页面
- 移动浏览器上未加载YouTube视频
- 加载YouTube视频并收听onPlayerStateChange
- 动态加载YouTube JSON API
- 引导modal won't加载youtube嵌入源
- 当CSS属性溢出改变时,在firefox上重新加载Youtube视频
- 使用API加载YouTube视频
- 我如何检查爆米花js是否无法加载YouTube视频
- 是否有一种方法来加载YouTube iframe后,内容已经完全加载
- 通过插件加载YouTube Iframe时,Chrome不安全的javascript请求
- 无法在框架中加载 youtube api
- 错误加载youtube视频在模态对话框与chrome
- 加载Youtube视频时不全屏随机
- 如何加载YouTube视频内联搜索时向下滚动
- 加载youtube嵌入mouseenter和静音