Selenium + Python - 通过execute_script检查图像
P粉899950720
P粉899950720 2024-04-03 09:32:15
0
1
376

我需要使用 python 中的 selenium 验证图像是否显示在页面上。 例如,让我们检查 https://openweathermap.org/ 页面左上角的徽标。 我使用 execute_script ,我的代码是:

def test_image(driver):
    driver.get('https://openweathermap.org/')
    time.sleep(10)
    image = driver.find_element(By.CSS_SELECTOR, "#first-level-nav > li.logo > a > img")
    print(image)
    print(driver.execute_script("return (typeof arguments[0].naturalWidth!=\"undefined\");", image))
    print(driver.execute_script("return (typeof arguments[0].naturalWidth>0);", image))
    print(driver.execute_script("return (arguments[0].naturalWidth);", image))

我得到了这个结果:

True
False
431

为什么 typeof argument[0].naturalWidth>0False,而 arguments[0].naturalWidth431?并且图像在页面上正确呈现。

更新:正确的代码是:

print(driver.execute_script("return (arguments[0].naturalWidth>0);", image))

P粉899950720
P粉899950720

全部回复(1)
P粉765570115

typeof 运算符优先于 >

typeof 431     === "number"
"number"   > 0 === false
typeof 431 > 0 === false
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!