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

[BUG] Finding elements by ID when ID contains dot fails #368

Open
1 task done
Heavenfighter opened this issue Jan 9, 2025 · 7 comments
Open
1 task done

[BUG] Finding elements by ID when ID contains dot fails #368

Heavenfighter opened this issue Jan 9, 2025 · 7 comments
Labels
bug Something isn't working

Comments

@Heavenfighter
Copy link
Contributor

✔️ Expected Behaviour

Find element with given ID when ID contains a dot.

🐞 Actual Behaviour

Function web_find do not find elemens when the selector type is ID and the selector_value contains a dot. As in https://api.jquery.com/category/selectors/ mentioned, some meta character need to be escaped with two backslashes '\'.

📋 Steps to Reproduce

Call function that uses web_find with selector_type ID and selector_value containing an dot for example.

📺 What browsers are you seeing the problem on? (if applicable)

No response

💻 What operating systems are you seeing the problem on? (if applicable)

No response

📃 Relevant log output (if applicable)

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Heavenfighter Heavenfighter added the bug Something isn't working label Jan 9, 2025
@Heavenfighter
Copy link
Contributor Author

I'm not sure if this should be generally solved in function web_find when selector_type is ID.
I have fixed this local in #370 only for setting special attributes, because i have seen no other id with dots or other meta characters in the page so far.

@sebthom
Copy link
Contributor

sebthom commented Jan 9, 2025

I would try escape the dots only when selector is ID at

match selector_type:
case By.ID:
return await self.web_await(
lambda: self.page.query_selector(f"#{selector_value}", parent),

for example something like:

lambda: self.page.query_selector(f"#{selector_value.replace('.', '\\.')}", parent)

@Heavenfighter
Copy link
Contributor Author

So do you also think that it wouldn't be problematic to generally replace the ID in web_find function?
And what do you think about the other metachars?

@sebthom
Copy link
Contributor

sebthom commented Jan 9, 2025

I think it is ok to change it for the selector by ID. I would not do this for the other selector types.
are there other metachars that need escaping?

@Heavenfighter
Copy link
Contributor Author

Please take a look at the link i provided. If the other chars are used in IDs by kleinanzeigen, i dont know. I find only the dots used in the special attributes so far.

@sebthom
Copy link
Contributor

sebthom commented Jan 9, 2025

I applied a fix in the preview release. please try. I think the other chars are really corner cases but doesn't hurt trying to escape them.

@Heavenfighter
Copy link
Contributor Author

This seems to work, thank you. But the workaround also has to be removed:

# workaround for https://github.com/Second-Hand-Friends/kleinanzeigen-bot/issues/368
elem_id = getattr(special_attr_elem.attrs, 'id')
elem_id = ''.join(['\\' + c if c in '!"#$%&\'()*+,./:;<=>?@[\\]^`{|}~' else c for c in elem_id])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants