Selenium + Python – 通过 execute_script 检查图像

huangapple go评论66阅读模式
英文:

Selenium + Python - Checking image via execute_script

问题

I need to verify if a image is shown on page with selenium in python.
For example lets check logo in left upper corner of the https://openweathermap.org/ page.
I use execute_script and my code is:

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))

And I got this results:

True
False
431

Why typeof arguments[0].naturalWidth>0 is False when arguments[0].naturalWidth is 431? And image rendered correctly on the page.

UPDATE: Correct code is:

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

I need to verify if a image is shown on page with selenium in python.
For example lets check logo in left upper corner of the https://openweathermap.org/ page.
I use execute_script and my code is:

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))

And I got this results:

True
False
431

Why typeof arguments[0].naturalWidth>0 is False when arguments[0].naturalWidth is 431? And image rendered correctly on the page.

UPDATE: Correct code is:

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

答案1

得分: 1

The typeof operator takes precedence over >.

typeof 431     === "number"
"number"   > 0 === false
typeof 431 > 0 === false
英文:

The typeof operator takes precedence over >.

typeof 431     === "number"
"number"   > 0 === false
typeof 431 > 0 === false

huangapple
  • 本文由 发表于 2023年5月14日 23:11:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定