João Santos
João Santos

Reputation: 194

How to use Scarpy Selector to get a value related with an id?

Following the html source code example:

<div id="form1">
    <input type="hidden" name="env_x" id="env_id_x" value="Value1">                             
    <input type="hidden" name="env_y" id="env_id_y" value="Value2">
    <input type="hidden" name="env_z" id="env_id_z" value="Value3">
    <input type="hidden" name="env_w" id="env_id_w" value="Value4">

If I want to get the value associated with a certain id how should I do it without having to iterate over all the inputs?

r = response.css('div[id="form1"]').css(input::attr(id)).getall()
# ["env_id_x","env_id_y","env_id_z","env_id_w"]

Now what i want is the value associated with the id="env_id_z" -> Value3

Kind Regards,

João

Upvotes: 1

Views: 54

Answers (1)

Md. Fazlul Hoque
Md. Fazlul Hoque

Reputation: 16187

Try:

r = response.css('div[id="form1"]').css('input#env_id_z::attr(value)').get()

Upvotes: 1

Related Questions