一个使用firefox和selenium点击js获取更多评论的小脚本
点击前只有10个评论,点击10次后可以获取110个评论了
package wdtest; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class wdtest { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.get("http://finance.sina.com.cn/chanjing/gsnews/20140416/061218811462.shtml"); Thread.sleep(2000); JavascriptExecutor jse = (JavascriptExecutor)driver; WebElement element = driver.findElement(By.xpath("//div[@id=‘J_Comment_More_Latest‘]/a")); Thread.sleep(10000); for(int i=0;i<10; i++) { jse.executeScript("arguments[0].click();", element); } try { Thread.sleep(1000000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } }