如何获取图像源值

时间:2022-11-28 21:46:18

Let's say I have HTML like this:

假设我有这样的HTML:

<div class="out-radio">
    <div class="in-radio">
        <input type="radio" value = "12345">
        <label
        <span           
            <div
                <img src="http://s3.amazonaws.com/test001.jpg">
            </div>
        </span
    </label
    </div>
    <div class="in-radio">
        <input type="radio" value = "12346">
        <label
        <span           
            <div
                <img src="http://s3.amazonaws.com/test002.jpg">
            </div>
        </span
    </label
    </div>
    <div class="in-radio">
        <input type="radio" value = "12347">
        <label
        <span           
            <div
                <img src="http://s3.amazonaws.com/test003.jpg">
            </div>
        </span
    </label
    </div>
    <div class="in-radio">
        <input type="radio" value = "12348">
        <label
        <span           
            <div
                <img src="http://s3.amazonaws.com/test004.jpg">
            </div>
        </span
    </label
    </div>
    <div class="in-radio">
        <input type="radio" value = "12349">
        <label
        <span           
            <div
                <img src="http://s3.amazonaws.com/test005.jpg">
            </div>
        </span
    </label
    </div>
</div>

If I want to display the value for the selected radio button, I would do it like this:

如果我想显示所选单选按钮的值,我会这样做:

browser.element(:xpath, '//*[@class = "in-radio"]').inputs.each {|input| puts value if input.checked?}

How would I display the src for the selected radio button? Is there any way I could put:

如何显示所选单选按钮的src?有什么方法我可以:

browser.element(:xpath, '//*[@class = "in-radio"]').(inputs,imgs).each {|input,img| ...}

1 个解决方案

#1


5  

If you select the last radio button:

如果选择最后一个单选按钮:

browser.radios.each {|radio| p radio.parent.img.src if radio.set?}
# => "http://s3.amazonaws.com/test005.jpg"

#1


5  

If you select the last radio button:

如果选择最后一个单选按钮:

browser.radios.each {|radio| p radio.parent.img.src if radio.set?}
# => "http://s3.amazonaws.com/test005.jpg"