我需要在python中使用硒替换src标记中的img属性。

浏览器是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/

10-14 15:34
查看更多