0

I'm trying to test a background-image with Selenium. It doesn't have <img> tag, it have this html code (it is banner from https://openweather.co.uk/products page):

<div class="current-img white-text half-section-right main-section-desktop"></div>

and this CSS styles

This JavaScript code works well in DevTools console:

const image = new Image(); 
image.src = 'https://openweather.co.uk/themes/openweather_b2b/assets/img/forecast-banner.jpg';
image.onload = function(){
const imageWidth = this.width; 
const imageHeight = this.height;
console.log('Width:', imageWidth, 'Height:', imageHeight);
};

But how to implement this JS code to driver.execute_script and pass imageWidth and imageHeight back to Python code?

If I just pass this code into driver.execute_script("here") and print it - I got None.
If replace last line with return('Width:', imageWidth, 'Height:', imageHeight); I got None too.

And in general - how to return data from driver.execute_script back to Python correctly?
For example driver.execute_script("return console.log('Hi');") gives None too.

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
zaelcovsky
  • 23
  • 4
  • Instead of complicating everything, you could simply use element.value_of_css_property(property_name) to get the css style values of
    tag & verify with your expected values. Documentation for this method usage can be found in https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webelement.WebElement.value_of_css_property
    – Srinivasu Kaki Jun 01 '23 at 01:31

0 Answers0