Cannot select element using both id and class

Bug #1717850 reported by Stephen James
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Beautiful Soup
Fix Released
Low
Unassigned

Bug Description

When trying to use Tag.select with both an id and class simultaneously, matches get missed.

Example:
```
from bs4 import BeautifulSoup

html = '<div id="sid" class="scls">Hello World!</div>'

soup = BeautifulSoup(html) # the issue is present regardless of the parser used

print(soup.select("#sid")) # works fine
print(soup.select(".scls")) # works fine
print(soup.select("#sid.scls")) # unexpectedly gives an empty list
print(soup.select(".scls#sid")) # also gives an empty list
```

The issue, as far as I see: if there is a "#", [the code](https://bazaar.launchpad.net/~leonardr/beautifulsoup/bs4/view/head:/bs4/element.py#L1400) assumes that there will only be a tag name and an id without the possibility of one or more classes as well. A possible fix would be to add an `elif '.' in token and '#' in token:` clause before to handle that case.

Tags: css
Revision history for this message
Leonard Richardson (leonardr) wrote :

Thanks for reporting this bug and for going deeper to suggest a solution. The CSS selector system is contributed code and for my own sanity I only add to it when a patch and test are contributed. I'm going to leave this issue open in a 'confirmed' state and if someone provides a patch or pull request I'll merge it.

Changed in beautifulsoup:
status: New → Confirmed
tags: added: css
Changed in beautifulsoup:
importance: Undecided → Low
Revision history for this message
Leonard Richardson (leonardr) wrote :

In the forthcoming 4.7.0 release, all CSS selector logic is delegated to the Soup Sieve project (https://facelessuser.github.io/soupsieve/). This should dramatically improve the overall support for CSS selectors in Beautiful Soup, and provide a responsive channel for improvements.

I'm closing this issue as of the 4.7.0 release. If it's still a problem in 4.7.0, file a bug against Soup Sieve. However, Isaac currently believes it's not a bug at all, so he'd need some more information.

Changed in beautifulsoup:
status: Confirmed → Fix Committed
Revision history for this message
Leonard Richardson (leonardr) wrote :

Scratch that bit about 'Isaac currently believes it's not a bug at all' -- that's a copy and paste error.

Revision history for this message
Leonard Richardson (leonardr) wrote :

In 4.7.0 release.

Changed in beautifulsoup:
status: Fix Committed → Fix Released
Revision history for this message
Stephen James (stephenorjames) wrote :

Thanks for following up on this, I can confirm that it now seems to work properly.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.