Scrapling icon indicating copy to clipboard operation
Scrapling copied to clipboard

css selector parser problem

Open crucifyer opened this issue 3 months ago • 6 comments

Have you searched if there an existing issue for this?

  • [x] I have searched the existing issues

Python version (python --version)

Python 3.14.7s

Scrapling version (scrapling.version)

0.4.15

Dependencies version (pip3 freeze)

anyio==4.14.1 apify_fingerprint_datapoints==0.15.0 asttokens==3.0.1 beautifulsoup4==4.15.0 browserforge==1.2.4 certifi==2026.6.17 cffi==2.1.0 click==8.4.2 cssselect==1.5.0 curl_cffi==0.16.1 decorator==5.3.1 executing==2.2.1 greenlet==3.5.3 idna==3.18 ipython==9.15.0 ipython_pygments_lexers==1.1.1 jedi==0.20.0 lxml==6.1.1 markdown-it-py==4.2.0 markdownify==1.2.3 matplotlib-inline==0.2.2 mdurl==0.1.2 msgspec==0.21.1 orjson==3.11.9 parso==0.8.7 patchright==1.62.1 pexpect==4.9.0 pip==26.2.1 playwright==1.62.0 prompt_toolkit==3.0.52 Protego==0.6.2 psutil==7.2.2 ptyprocess==0.7.0 pure_eval==0.2.3 pycparser==3.0 pyee==13.0.1 Pygments==2.20.0 rich==15.0.0 scrapling==0.4.15 six==1.17.0 soupsieve==2.8.4 stack-data==0.6.3 tld==0.13.2 traitlets==5.15.1 typing_extensions==4.16.0 w3lib==2.4.1 wcwidth==0.8.2

What's your operating system?

mac 26.6.2

Are you using a separate virtual environment?

Yes

Expected behavior

cannot use a period (.) in an ID selector.

% scrapling extract stealthy-fetch 'https://scrapling.readthedocs.io/en/latest/api-reference/response.html#scrapling.engines.toolbelt.custom.Response.markdown' test.md -s 'div:has(>#scrapling\\.engines\\.toolbelt\\.custom\\.Response\\.markdown)'
[2026-09-01 19:41:37] INFO: Fetched (200) <GET https://scrapling.readthedocs.io/en/latest/api-reference/response.html#scrapling.engines.toolbelt.custom.Response.markdown> (referer: https://www.google.com/)
Traceback (most recent call last):
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/scrapling/parser.py", line 610, in css
    xpath_selector = _css_to_xpath(selector)
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/scrapling/core/translator.py", line 134, in css_to_xpath
    return translator.css_to_xpath(query)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/scrapling/core/translator.py", line 124, in css_to_xpath
    return super().css_to_xpath(css, prefix)
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/cssselect/xpath.py", line 221, in css_to_xpath
    for selector in parse(css)
                    ~~~~~^^^^^
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/cssselect/parser.py", line 545, in parse
    return list(parse_selector_group(stream))
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/cssselect/parser.py", line 560, in parse_selector_group
    yield Selector(*parse_selector(stream))
                    ~~~~~~~~~~~~~~^^^^^^^^
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/cssselect/parser.py", line 569, in parse_selector
    result, pseudo_element = parse_simple_selector(stream)
                             ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/cssselect/parser.py", line 703, in parse_simple_selector
    combinator, arguments = parse_relative_selector(stream)
                            ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/Users/myuser/PycharmProjects/scrapling/venv/lib/python3.14/site-packages/cssselect/parser.py", line 763, in parse_relative_selector
    raise SelectorSyntaxError(f"Expected an argument, got {next_}")
cssselect.parser.SelectorSyntaxError: Expected an argument, got <HASH 'scrapling\' at 9>
% scrapling extract stealthy-fetch 'https://scrapling.readthedocs.io/en/latest/api-reference/response.html#scrapling.engines.toolbelt.custom.Response.markdown' test.md -s 'div:has(>#scrapling\.engines\.toolbelt\.custom\.Response\.markdown)' 
...
cssselect.parser.SelectorSyntaxError: Invalid CSS selector 'div:has(>#scrapling\.engines\.toolbelt\.custom\.Response\.markdown)': Expected an argument, got <HASH 'scrapling.engines.toolbelt.custom.Response.markdown' at 9>
scrapling extract stealthy-fetch 'https://scrapling.readthedocs.io/en/latest/api-reference/response.html#scrapling.engines.toolbelt.custom.Response.markdown' test.md -s 'div:has(>#scrapling\\\.engines\\\.toolbelt\\\.custom\\\.Response\\\.markdown)'
...
cssselect.parser.SelectorSyntaxError: Expected an argument, got <HASH 'scrapling\.engines\.toolbelt\.custom\.Response\.markdown' at 9>
scrapling extract stealthy-fetch 'https://scrapling.readthedocs.io/en/latest/api-reference/response.html#scrapling.engines.toolbelt.custom.Response.markdown' test.md -s 'div:has(>#scrapling\\\\.engines\\\\.toolbelt\\\
\.custom\\\\.Response\\\\.markdown)'
...
cssselect.parser.SelectorSyntaxError: Expected an argument, got <HASH 'scrapling\\' at 9>

Actual behavior

This syntax works in a browser.

> document.querySelectorAll('div:has(>#scrapling\\.engines\\.toolbelt\\.custom\\.Response\\.markdown)')
< NodeList [div.doc.doc-object.doc-function]0: div.doc.doc-object.doc-functionlength: 1[[Prototype]]: NodeList

Steps To Reproduce

You can test this from the command line, and you'll get the same result with Python code.

scrapling extract stealthy-fetch 'https://scrapling.readthedocs.io/en/latest/api-reference/response.html#scrapling.engines.toolbelt.custom.Response.markdown' test.md -s 'div:has(>#scrapling\\.engines\\.toolbelt\\.custom\\.Response\\.markdown)'

thank you.

crucifyer avatar Sep 01 '26 10:09 crucifyer

It should also be possible to specify a timeout for the wait selector.

% scrapling extract stealthy-fetch "https://scrapling.readthedocs.io/en/latest/api-reference/fetchers.html" timeout.md --solve-cloudflare --timeout=10000 --wait 500 --wait-selector ".notFoundClass"
[2026-09-01 20:11:27] ERROR: No Cloudflare challenge found.
[2026-09-01 20:12:27] ERROR: Error waiting for selector .notFoundClass: Locator.wait_for: Timeout 60000ms exceeded.
Call log:
  - waiting for locator(".notFoundClass").first

[2026-09-01 20:12:28] INFO: Fetched (200) <GET https://scrapling.readthedocs.io/en/latest/api-reference/fetchers.html> (referer: https://www.google.com/)
[2026-09-01 20:12:28] INFO: Content successfully saved to '/Users/myuser/PycharmProjects/scrapling/timeout.md'

crucifyer avatar Sep 01 '26 11:09 crucifyer

This is not a Scrapling bug. The CSS library it uses (cssselect) doesn't support #id inside :has() at all, even without the dots. Fix opened: scrapy/cssselect#187 Workaround for now:

page.css('[id="scrapling.engines.toolbelt.custom.Response.markdown"]')[0].parent

About the timeout: --timeout does work, but --solve-cloudflare silently bumps it to a 60s minimum. Drop that flag and your 10s timeout will kick in.

yetval avatar Sep 01 '26 14:09 yetval

@yetval Thank you for your reply. What if we restore the timeout value whenever the system detects that there is no Cloudflare challenge?

ERROR: No Cloudflare challenge found.

crucifyer avatar Sep 01 '26 14:09 crucifyer

@crucifyer I think that would work since the selector runs after that check, so the timeout could be reset there. The one catch is that the code overwrites your value with 60s instead of keeping it, so the fix would need to save it first. This would need to be up to @D4Vinci, of course.

yetval avatar Sep 01 '26 14:09 yetval

@yetval @crucifyer traced the timeout part through to where it actually gets lost, since it's not restored anywhere later:

# _validators.py, StealthConfig.__post_init__
if self.solve_cloudflare and self.timeout < 60_000:
    self.timeout = 60_000          # overwrites, doesn't save

That value feeds page.set_default_timeout() before the page ever loads, so by the time wait_selector runs, the original 10s is gone and it inherits the 60s floor — same run either way, whether or not a challenge actually showed up.

solve_cloudflare=False   timeout=10000 -> config.timeout=10000
solve_cloudflare=True    timeout=10000 -> config.timeout=60000

Three ways to fix it, roughly in order of how much they touch:

  1. Save it and restore it once the solver returns without finding a challenge — closest to what's been discussed here, but there are a few early-return paths in the solver, so "restore where" needs picking carefully.
  2. Don't clobber config.timeout at all — give the Cloudflare solver its own internal budget instead of borrowing the page timeout. Bigger diff, but it's the one that stops this from being a landmine for the next thing that reads config.timeout downstream.
  3. Give wait_selector its own timeout — narrower, fixes the symptom in this issue without touching the solver, but leaves the underlying overwrite for the next person to trip over.

Happy to put together whichever one you'd rather have — didn't want to guess and build the wrong shape.

(Disclosure per AI_POLICY.md: found and traced with Claude Code.)

yamantaka-singh avatar Sep 03 '26 17:09 yamantaka-singh

Thanks for the help, @yetval. Yes, please use the workaround provided till csselect releases the fix.

As @yetval said, the Cloudflare flag overwrites it, but restoring the timeout when Cloudflare isn't present is a very rare edge case that doesn't warrant adding to the codebase for everyone else.

The page_action parameter is executed after the Cloudflare solver execution finishes, so you can use it to access the page object and set the timeout as you want after the Cloudflare check is finished.

D4Vinci avatar Sep 12 '26 15:09 D4Vinci