Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronous Driver Actions Solution to README #106

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ def parse_result(self, response):
```
For more information about the available driver methods and attributes, refer to the [selenium python documentation](http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webdriver)

Ideally you do not want any driver actions to be used during response parsing since scrapy design is that responses and requests are asynchronous and this package only utilizes a single webdriver. But sometimes you require that user actions such as clicks, forms, etc be done after request but prior to parsing so that response object has all data you intend to scrape. Here is a solution that may help in those circumstances:

1. create custom selenium wait condition (https://selenium-python.readthedocs.io/waits.html)
```python
class wait_title(object):
def __init__(self):
self.params = "string"

def __call__(self, driver):
# driver actions go here ...
title = driver.title
if title == self.params:
return title #technically not matter what you return since this package ignores it anyways ... just no boolean if success condition
else
return False #if returns false then continues waiting and will run this function again after some delay
```
2. then supply this custom wait condition to selenium request like so:
```python
SeleniumRequest(url=url, callback=self.parse, wait_until=wait_title(), wait_time=10)
```

This will ensure that a given request performs all requisite driver actions before formulating a response object to be parse asynchronously.

The `selector` response attribute work as usual (but contains the html processed by the selenium driver).
```python
def parse_result(self, response):
Expand Down