You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func testExample()throws{lethtml="""<div class="models"><a class="model" href="https://cat.com"><img title="" data-original-title="NAME-A"></a><a class="model" href="https://duck.com"><span title="" data-original-title="NAME-B"></a></div>"""letdoc:Document=trySwiftSoup.parse(html)letcreatives=try doc.select(".models .model")
for creative in creatives {letname=try creative.select(":first-child")print(try name.attr("data-original-title"))}}
The code above works correctly and prints
NAME-A
NAME-B
However, if the creative.select(":first-child") part was replaced by creative.select(":first-child").first()!, then the first child will return the parent element a:
...
for creative in creatives {letname=try creative.select(":first-child").first()!
print(try name.attr("data-original-title"))}...
prints an empty string and NAME-B
NAME-B
The text was updated successfully, but these errors were encountered:
Please see the example below:
The code above works correctly and prints
However, if the
creative.select(":first-child")
part was replaced bycreative.select(":first-child").first()!
, then the first child will return the parent elementa
:prints an empty string and NAME-B
The text was updated successfully, but these errors were encountered: