Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/pyscrappy/generic/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def _values(self) -> list[str]:
if self._pseudo == "text":
return [s.text() for s in self]
if self._pseudo == "attr":
return [str(s.attrs.get(self._attr, "")) for s in self]
return [str(s.attrs[self._attr]) for s in self if self._attr in s.attrs]
return [s.html() for s in self]

def get(self, default: str | None = None) -> str | None:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_generic/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def test_css_text_pseudo_get_and_getall(self):
def test_css_attr_pseudo(self):
assert _page().css("a::attr(href)").getall() == ["/a", "/b"]

def test_css_attr_missing_attribute_uses_default(self):
assert Selector("<a>x</a>").css("a::attr(href)").get("MISSING") == "MISSING"

def test_css_attr_getall_skips_missing_attributes(self):
html = "<a href='/present'>yes</a><a>missing</a>"
assert Selector(html).css("a::attr(href)").getall() == ["/present"]

def test_css_chaining(self):
first = _page().css(".product")[0]
assert first.css(".price::text").get() == "$10"
Expand Down
Loading