Open
Description
Please see the example below:
func testExample() throws {
let html = """
<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>
"""
let doc: Document = try SwiftSoup.parse(html)
let creatives = try doc.select(".models .model")
for creative in creatives {
let name = 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 {
let name = try creative.select(":first-child").first()!
print(try name.attr("data-original-title"))
}
...
prints an empty string and NAME-B
NAME-B
Activity