Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get img src in string in selenium using python

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?

like image 463
cosminp Avatar asked Jul 20 '17 13:07

cosminp


People also ask

How do I get the src of an image in Selenium?

To fetch any attribute in Selenium, we have to use the getAttribute() method. The method takes the attribute name as a parameter. So to get the src attribute, we have to write getAttribute("src").

How use xpath IMG tag in Selenium?

WebElement temp = driver. findElement(By. xpath("//img[contains(@src,'web/L001/images/IMAGENAME. jpg')]"));


2 Answers

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")

like image 92
Gaurang Shah Avatar answered Sep 30 '22 17:09

Gaurang Shah


Try:

src = driver.find_element_by_xpath("//div[@class='rc]/h3[@class='r']/a/img").get_attribute("src")

P.S.- Used full available xpath to avoid duplicate conflicts on actual DOM.

like image 34
Rahul Chawla Avatar answered Sep 30 '22 16:09

Rahul Chawla