我需要使用 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>0 是 False,而 arguments[0].naturalWidth 是 431?并且图像在页面上正确呈现。
更新:正确的代码是:
print(driver.execute_script("return (arguments[0].naturalWidth>0);", image))
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
typeof运算符优先于>。