mySun
mySun

Reputation: 1696

How to change attribute in selenium in python?

I need change src attribute in img tag with selenium in python.

Browser is firefox.

HTML code:

<div style="" id="image_sec">
    <img src="city.png" alt="">
</div>

need change to:

<div style="" id="image_sec">
    <img src="new_city.png" alt="">
</div>

Python code:

driver.execute_script('$("#image_sec img").innerHtml(<img src="new_city.png" alt="">);')

But this code is not work :-(

Upvotes: 2

Views: 3079

Answers (1)

Andersson
Andersson

Reputation: 52665

Try to implement below line

driver.execute_script('document.querySelector("#image_sec>img").src="new_city.png";')

or

driver.execute_script('document.querySelector("#image_sec>img").setAttribute("src", "new_city.png");')

Upvotes: 3

Related Questions