0

So my problem is that i need to edit this image source in selenium, but it doesn't have id or class.

<div id="mbr-content">
            <div class="nope" some random stuff>
    <script>
    </script>

        <div class="mbr-image-container">
            <div class="mbr-image-wrapper">
                <div class="mbr-image">
                    <img src="this source need to modify" alt="app_image">
                </div>
            </div>
        </div>
    #Html continues here

I just in some reason cant figure this one out.

I know that I need to use this command but not sure what shut I putt inside as script.

driver.execute_script("something here")

Using python-3.7

3
  • "I have tried somethings..." which "things" exactly? Commented Aug 24, 2018 at 10:27
  • That is hard to say because it's been couple weeks last time I tried this and I was close to get it work. If remember them I will edit them over there. Commented Aug 24, 2018 at 10:36
  • You mean say changing some HTML attribute using execute_script ? Commented Aug 24, 2018 at 11:03

2 Answers 2

2

The javascript required to set the src attribute of your <img> element is:

document.querySelector(".mbr-image > img").src="whatver you want";

So, you can try below solution:

js = 'document.querySelector(".mbr-image > img").src="whatver you want";'
driver.execute_script(js)
Sign up to request clarification or add additional context in comments.

Comments

2

As per the HTML you have shared to edit the src attribute of the desired element you can use the following solution:

element = driver.find_element_by_xpath("//div[@class='mbr-image-container']/div[@class='mbr-image-wrapper']/div[@class='mbr-image']/img[@alt='app_image']")
driver.execute_script("arguments[0].setAttribute('src','something here')", element)

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.