1

I want to grab everything within this div with an id of "storytext":

<!-- END ID="STORY-META" CLASS="STORY-META" -->
<div id="storytext" class="storytext storylocation linkLocation">
      <p>NPR's Ari Shapiro talks with <em>Washington Post</em> reporter David Weigel about the conspiracy behind Democratic National Committee staffer Seth Rich's murder last summer and how fake news is still spreading.</p>
</div>
<div class="transcript storytext" aria-label="Transcript">
    <b class="icn-story-transcript-wrap">
        <b class="icn-story-transcript"></b>
    </b>

Trying to grab this div:

public void init(Properties p) {
    log.info(p.toString());
    String url = p.getProperty("url");
    String tagNameString = p.getProperty("tagNameString");
    WebDriver driver = new FirefoxDriver();
    driver.get(url);
    driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
    String pageSource = driver.getPageSource();
    log.info(pageSource);
    log.info("done");
    log.info(tagNameString);
    List<WebElement> listOfWebElements = driver.findElements(By.tagName(tagNameString));
    log.info(listOfWebElements.toString());
}

Execution log:

    window.onload = (typeof window.onload != 'function') ?
            loadChartbeat : function() {
        oldonload();
        loadChartbeat();
    };
})();
</script><script language="javascript" type="text/javascript" src="https://static.chartbeat.com/js/chartbeat.js"></script></body></html>
Jul 12, 2017 8:11:59 PM net.bounceme.dur.web.selenium.Scraper init
INFO: done
Jul 12, 2017 8:11:59 PM net.bounceme.dur.web.selenium.Scraper init
INFO: storytext
Jul 12, 2017 8:12:12 PM net.bounceme.dur.web.selenium.Scraper init
INFO: []
thufir@doge:~/NetBeansProjects/selenium$ 

whether to iterate, parse or persist. Playing with Selenium.

2
// tagNameString is equal to "storytext"
driver.findElements(By.tagName(tagNameString)); 

What you are actually asking Selenium to do is to find all "storytext" elements. Selenium will be looking for <storytext>...</storytext> elements which, I am pretty sure, you have none.

Instead, you intended to use the "by id" locator:

WebElement myElement = driver.findElement(By.id(tagNameString));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.