java - How to read youtube comments using Selenium? -


i'm trying read youtube video comments using following code:

firefoxdriver driver = new firefoxdriver(); driver.get("https://www.youtube.com/watch?v=jcbbnpykuw4");  webelement element = driver.findelementbycssselector("#watch-discussion"); system.out.println(element.gettext()); // prints: loading..  // scrolll down comments start load driver.executescript("window.scrollby(0,500)", "");  thread.sleep(10000);  element = driver.findelementbycssselector("#watch-discussion"); system.out.println(element.gettext()); 

last statement prints empty string. why?

it little tricky because comments written in separate iframe tag inside watch discussion. have switch on iframe first using driver.switchto().frame("put id or name here"); iframe id random value. after switch iframe can find comments comments in div have class name 'ct' can using xpath. see below working code

firefoxdriver driver = new firefoxdriver(); driver.get("https://www.youtube.com/watch?v=jcbbnpykuw4");  webelement element = driver.findelementbycssselector("#watch-discussion"); system.out.println(element.gettext()); // prints: loading..  // scrolll down comments start load driver.executescript("window.scrollby(0,500)", "");  thread.sleep(20000);  list<webelement> iframes = driver.findelements(by.xpath("//iframe"));  for(webelement e : iframes) {            if(e.getattribute("id") != null && e.getattribute("id").startswith("i0_")) {     // switch iframe contains comments     driver.switchto().frame(e);     break;     } }  // fetch comments list<webelement> comments = driver.findelements(by.xpath("//div[@class='ct']")); for(webelement e : comments) {           system.out.println(e.gettext()); } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -