How to get img src in string in selenium using python

cosminp picture cosminp · Jul 20, 2017 · Viewed 23.9k times · Source

I have a google search result, something like

div class="rc"
    h3 class="r"
        <a href="somelink">
            <img id="idFOO" src="data:somethingFOO" style="...">
        </a>

I want to add somethingFOO (or data:somethingFOO) to a string using python & selenium. How can I do that?

Answer

Gaurang Shah picture Gaurang Shah · Jul 20, 2017

what you are interested is not a text, it's an attribute with name src. so if you will do something like that, you won't get what you want.

find_element_by_id("idFOO").text

if your html is like this,

<input id="demo">hello world </input>

then the following code will give you hello world

driver.find_element_by_id("demo").text

and following code will give you demo

driver.find_element_by_id("demo").get_attribute("id")

so in your case, it should be

driver.find_element_by_id("idFOO").get_attribute("src")