Search code examples
web-crawlerscrapyscrapy-splash

Selecting elements with scrapy-splash and screenshot after


When using scrapy-splash I am trying to go to a dropdown and select "Vanilla" in this case. After selecting from the dropdown I need to take a screenshot which I currently have working. Here is what I have so far.

class TestSpider(scrapy.Spider):
    name = 'test'

    def start_requests(self):
        splash_args = {
            'png': 1,
            'render_all': 1,
            'wait': 2,
        }
        url = 'https://developer.mozilla.org/en-US/docs/Web/Events/change'

        yield SplashRequest(
            url,
            endpoint='render.html',
            args=splash_args
        )
        yield scrapy.Request(
            f"http://192.168.99.100:8050//render.png?url={url}&wait=2&render_all=1",
            self.parse_request,
        )

    def parse_request(self, response):
        with open('request.png', 'wb') as f:
            f.write(response.body)

Solution

  • You can pass a js script to run in your splash_args through the js_source argument. In your case you may want to do something like this:

    splash_args = {
       'png': 1,
       'render_all': 1,
       'wait': 2,
       'js_source': 'document.getElementById("mySelect").value = "Vanilla";'
    }