12

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?

5
  • what do you mean you wan to add, do you mean you want to fetch this text and combine with string or you want to add something to this text ? Commented Jul 20, 2017 at 13:21
  • I want to put "somethingFOO" in a string. For example I want to be able to do "return string" and the result to be "somethingFOO". Commented Jul 20, 2017 at 13:23
  • what have you tried so far, and what's not working? Commented Jul 20, 2017 at 13:25
  • I've tried myString=browser.find_element_by_class_name("r").text but I don't get what I want. I get some other text. I've tried find_element_by_id("idFOO).text and I get nothing. Commented Jul 20, 2017 at 13:27
  • Possible duplicate of accessing selenium web elements with python Commented Jul 20, 2017 at 15:39

2 Answers 2

39

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

Sign up to request clarification or add additional context in comments.

3 Comments

@cosminp you could select the answer, if it's working so it help others.
Just did that. :) Quick question if I may, can I do something to look only for a substring in driver.find_element_by_id("idFOO")? I'd like something like driver.find_element_by_id("idF*").
yes, driver.find_element_by_xpath("//*[contains(@id,'idF')]")
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.