Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

UPDATE: Correct code is:

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

>Solution :

The typeof operator takes precedence over >.

typeof 431     === "number"
"number"   > 0 === false
typeof 431 > 0 === false
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading