1

Image

As you can see in the image, I have 2 solutions (solution 1 is for a flight and solution 2 is for a hotel)

I want to iterate over each solution using XPATH. I am successful in that. I want to check the image returned on iteration.

My code is somewhat like this:

if select_solution.size != 0:
    print(str(i) + " solution selected")
    if '/assets/review/images/nodes/icon-plane.png' in driver.find_element(By.XPATH, config.RT_flightImage_check_Xpath.format(i)).get_attribute("src"):
        result['Solution ' + str(i)] = {}
                            print("Solution is for Flight")
                            print("**********************")
                            returned_solution = 'Flight'
                            result['Solution ' + str(i)]['Returned Solution'] = returned_solution
                        
elif 'assets/review/images/nodes/icon-hotel.png' in driver.find_element(By.XPATH, config.RT_HotelImage_check_Xpath.format(i)).get_attribute("src"):
                        result['Solution ' + str(i)] = {}
                        print("Solution is for Hotel")
                        print("**********************")
                        returned_solution = 'Hotel'
                        result['Solution ' + str(i)]['Returned Solution'] = returned_solution

Result:

Total number of Solutions found:  2
1 solution selected
Solution is for Flight
**********************
--------
2 solution selected
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//hgb-content-view[1]/div[1]/div[1]/div[2]/div[1]/hgb-flight-content-view[1]/div[1]/div[2]/div[1]/img[1]"}

(Session info: chrome=103.0.5060.134)

The issue that i am facing is that when it iterates the 1st time, it finds the flight and does the necessary operation, but during the second iteration it fails because it does not find the flight (No such element exception).

How do I make sure that it goes to the elif and not exit cause the flight xpath is not found in the selected iteration?

  • 1) _goes to the elif_: Can you show us your goes to the elif 2) _No such element exception_: Update the question with the text based relevant HTML and the error stacktrace. – undetected Selenium Aug 01 '22 at 21:33
  • could it be the action causes the element to dissapear? – jspcal Aug 01 '22 at 21:36
  • @undetectedSelenium updated my question with code snippet – Rustom Contractor Aug 01 '22 at 21:42
  • **1)** Does `By.XPATH, config.RT_flightImage_check_Xpath.format(i)` resolves to `//hgb-content-view[1]/div[1]/div[1]/div[2]/div[1]/hgb-flight-content-view[1]/div[1]/div[2]/div[1]/img[1]` **2)** Can you crosscheck if `elif` section is properly indented? – undetected Selenium Aug 01 '22 at 21:58
  • @undetectedSelenium it does hence the output has the solution for that. Elif is also indented perfectly. I am not sure what else to do as the code fails on second iteration – Rustom Contractor Aug 02 '22 at 02:34

2 Answers2

0
try:
   elem=driver.find_element(By.XPATH, config.RT_flightImage_check_Xpath.format(i))
except Exception as e:
   print(str(e))
   elem=[]
   pass

Try except and catch the error and display it. Then do it for the other one.

if len(elem)!=0 and '/assets/review/images/nodes/icon-plane.png' in elem.get_attribute("src"):
    #your code
elif len(elem2)!=0 and 'assets/review/images/nodes/icon-hotel.png' in elem2.get_attribute("src"):
    #your code
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
  • This actually worked. Had to tweak it a little instead of find_element, I used find_elements ` try: flight_element=driver.find_elements(By.XPATH, config.RT_flightImage_check_Xpath.format(i)) except Exception as e: print(str(e)) elem=[] pass ` ` if len(flight_element) != 0 and '/assets/agent/icons/transaction/plane.svg' in driver.find_element(By.XPATH, config.AA_flight_icon_Xpath.format(i)).get_attribute("svg"): ` and this way, in second iteration i was able to find the hotel and get the desired result – Rustom Contractor Aug 02 '22 at 16:20
0

This actually worked. Had to tweak it a little instead of find_element, I used find_elements

try: 
    flight_element=driver.find_elements(By.XPATH, config.RT_flightImage_check_Xpath.format(i)) 
except Exception as e: 
    print(str(e)) elem=[] pass 
    
if len(flight_element) != 0 and '/assets/agent/icons/transaction/plane.svg' in driver.find_element(By.XPATH, config.AA_flight_icon_Xpath.format(i)).get_attribute("svg"):

and this way, in second iteration i was able to find the hotel and get the desired result

cottontail
  • 10,268
  • 18
  • 50
  • 51