Search code examples
htmlrweb-scrapingrselenium

Scraping "load more" buttom giving error - unable to locate element


I am trying to reproduce the code of the chosen answer from this post Issue scraping page with "Load more" button with rvest, on this website https://www.coindesk.com/. However, the following line is giving an error:

#original    
#load_btn <- ffd$findElement(using = "css selector", ".load-more .btn")
#modified
load_btn <- ffd$findElement(using = "css selector", ".load-more-stories .btn")

Selenium message:Unable to locate element: load-more-stories For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T21:30:10' System info: host: 'LAPTOP-sdsds9L', ip: 'sdssd', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_211' Driver info: driver.version: unknown

Error: Summary: NoSuchElement Detail: An element could not be located on the page using the given search parameters. class: org.openqa.selenium.NoSuchElementException Further Details: run errorDetails method

I assumed the buttom name based on the lines 449-452:

 </div>
            <div id="load-more-stories">
    <button>Load More Stories</button>
</div>        </div>

Any idea how to adapt this strategy properly?


Solution

  • You first need to dimiss cookie bar by clicking accept button, then move on to using the load-more-stories as the id, not class. I can't test in R but something like:

    cookie_button  <- ffd$findElement("css selector", '#CybotCookiebotDialogBodyLevelButtonAccept')
    cookie_button$clickElement()
    load_more_button  <- ffd$findElement("css selector", '#load-more-stories')
    load_more_button$clickElement()
    

    References:

    1. https://cran.r-project.org/web/packages/RSelenium/RSelenium.pdf