使用Selenium Python以空条件执行Jquery

问题描述

嗨,我尝试发送如下所示的javascript:

$("img[src|='']").attr('src',result.responseJSON.data);

将具有空值的图像更改为新值。我正在使用执行脚本,因此它看起来像这样:

driver.execute_script("$("img[src|='']").attr("src",result.responseJSON.data);}});")

driver.execute_script('$("img[src|='']").attr('src',result.responseJSON.data);}});')

但是它总是返回类似

错误

enter image description here

enter image description here

我也将其签名为python变量,并使用execute脚本调用它,但是没有运气,是否有任何修复建议? 预先感谢。

解决方法

最后我找到了答案,我应该在转义字符之前加上:

driver.execute_script("$(\"img[src|='']\").attr("src",result.responseJSON.data);")

它有效。