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?
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").
WebElement temp = driver. findElement(By. xpath("//img[contains(@src,'web/L001/images/IMAGENAME. jpg')]"));
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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With