我需要在 python 中使用 selenium 更改 img
标记中的 src
属性。
浏览器是 firefox
。
HTML
代码:
<div style="" id="image_sec">
<img src="city.png" alt="">
</div>
需要更改为:
<div style="" id="image_sec">
<img src="new_city.png" alt="">
</div>
Python
代码:
driver.execute_script('$("#image_sec img").innerHtml(<img src="new_city.png" alt="">);')
但是这段代码不起作用:-(
最佳答案
尝试实现以下行
driver.execute_script('document.querySelector("#image_sec>img").src="new_city.png";')
或
driver.execute_script('document.querySelector("#image_sec>img").setAttribute("src", "new_city.png");')
关于python - 如何在python中更改selenium的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48622500/