'How to automate weather forecast image using Selenium

I have been struggling to validate the weather forecast. So the scenario goes like- We have 7 cities in the dropdown & on selecting the respective city the system displays the weather icons

Steps to reproduce >

  1. Select any city from dropdown (eg.- Scotland)
  2. System displays the weather images (Sunny, Cloudy, Rainy or Partly cloudy) depending upon the response coming from the 3rd party system & these images are not consistent meaning if one moment Sunny weather image gets displayed then when we execute the system so some other weather image gets displayed lets say rainy

Lets say on selecting scotland I get the weather imag as Sunny So based on the above behavoir I am giving the condition as-

if(chiliflugb.partly_cloudy_weather_text.isDisplayed() || chiliflugb.sunny_weather_text.isDisplayed() ) { //System.out.println(chiliflugb.weather_condition_icon.getAttribute("alt")); System.out.println("displayed"); so in the if condition if im passing the Sunny weather condition as 2nd attribute then my script fails giving the error as >>

java.lang.AssertionError: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class='js-weather-condition-icon']//child::img[@alt='Partly cloudy']"} (Session info: chrome=101.0.4951.64) For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30' System info: host: 'GGNUWT448637L', ip: '192.168.193.29', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_331' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 101.0.4951.64, chrome: {chromedriverVersion: 100.0.4896.60 (6a5d10861ce8..., userDataDir: C:\Users\aditi.sharma21\App...}, goog:chromeOptions: {debuggerAddress: localhost:49942}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: eager, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true} Session ID: 23e05d169b5b3afe7befd2a319e9bf8b *** Element info: {Using=xpath, value=//span[@class='js-weather-condition-icon']//child::img[@alt='Partly cloudy']}

But again if Im passing the Sunny weather as 1st attribute then the script is getting passed

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source