lefthandedgoat / canopy

f# web automation and testing library, built on top of Selenium (friendly to c# also)
http://lefthandedgoat.github.io/canopy/
MIT License
505 stars 117 forks source link

Get Attribute value from element #502

Closed felipeotarola closed 4 years ago

felipeotarola commented 4 years ago

Hello.

Is it possible to retrieve an attribute value from an element? I have been trying different methods,

My dom element looks something like this.

`

<div class="Grid__cell"><a href="17ae0a.png" id="artimglink_47373113"> <img src="17ae0a.png_0_0_100_100_65_80_0.png"  id="artimg_47373113" ></a> </div>

<div class="Grid__cell"><a href="4ff9fc.png" id="artimglink_47373115"><img src="4ff9fc.png_0_0_100_100_65_80_0.png"  id="artimg_47373115" ></a> </div>

<div class="Grid__cell"><a href="3dcf91.png" id="artimglink_47373117"><img src="3dcf91.png_0_0_100_100_65_80_0.png"  id="artimg_47373117" ></a> </div>

<div class="Grid__cell"><a href="032205.jpg" id="artimglink_47373119"> <img src="032205.jpg_0_0_100_100_59_80_0.jpg"  id="artimg_47373119" ></a> </div>

`

Where I want to get the href value from the link.

I tried something like this let images = elements "//*[contains(@id, 'artimglink')]/@href" |> List.map read but that didn't obviously work.

Any ideas :)?

olivercoad commented 4 years ago

Try this

let images =
    elements "[id^=artimglink]"
    |> List.map (fun el -> el.GetAttribute("href"))
felipeotarola commented 4 years ago

Nice! Thank you. worked as a charm