Home > Back-end >  ElementNotInteractableException in python selenuim
ElementNotInteractableException in python selenuim

Time:01-21

So I am trying to scrape this page:

https://www.tripadvisor.com/CheapFlightsHome

but when ever I try to click on the choosing the flight class element it just gives this error:

  File "e:\code\Python\non machine learning projects\web scrabbing\Projects\flight-anlaysis\flight-anlaysis.py", line 128, in <module>
    extra_info("Economy" , 2  , 0 , 3)
  File "e:\code\Python\non machine learning projects\web scrabbing\Projects\flight-anlaysis\flight-anlaysis.py", line 79, in extra_info
    drop_down_btn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH , '//span[@class = "ui_icon caret-down open-close"]')))
  File "C:\Users\user\anaconda3\envs\mix\lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

the Python code:

def extra_info(wclass , noa , nos , noc):#noa : number of adults nos: number of seniors noc: number of children
    # CLicking the main button
    mbtn = driver.find_element(By.XPATH , '//span[@class = "summaryContainer target"]')
    mbtn.click()
    time.sleep(2)
    mdiv = driver.find_element(By.XPATH , '//div[@class = "prw_rup prw_flights_cos_passenger_picker cosPassengerPopover"]')
    time.sleep(2)
    mmdiv = mdiv.find_element(By.XPATH , '//div[@class = "popoverContents"]')
    wclassbtn = mmdiv.find_element(By.XPATH , '//div[@class = "picker-inner localizationCosStrings"]')
    drop_down_btn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH , '//span[@class = "ui_icon caret-down open-close"]')))
    time.sleep(5)
    drop_down_btn.click()
    plane_grades = []
    eco = driver.find_element(By.XPATH , '//@li[text() = "Economy"]')
    peco = driver.find_element(By.XPATH , '//@li[text() = "Premium Economy"]')
    bus = driver.find_element(By.XPATH , '//li[@text() = "Business Class"]')
    fc = driver.find_element(By.XPATH , '//li[@text = "First Class"]')
    plane_grades.append(eco , peco , bus , fc)
    for plane , i  in enumerate(plane_grades): # this for loop choses the class grade for the plane
        if plane == wclass:
            choosen_btn = plane_grades[i]
            choosen_btn.click()
    # checking now for the nos noa noc
    adult_counter_div = driver.find_element(By.XPATH , '//div[@class = "adultCounter counter"]')
    senior_counter_div = driver.find_element(By.XPATH , '//div[@class = "seniorCounter counter"]')
    child_counter_div = driver.find_element(By.XPATH , '//div[@class = "childrenCounter counter"]')
    if noa <= 6 and senior_counter_div<=6 and child_counter_div<=5: #Checking the count of the number of tickets  
        add_adult_btn = adult_counter_div.find_element(By.XPATH , '//span[@class = "ui_icon plus-circle enabled"]')
        add_senior_btn = senior_counter_div.find_element(By.XPATH , '//span[@class = "ui_icon plus-circle enabled"]')
        add_child_btn  = child_counter_div.find_element(By.XPATH , '//span[@class = "ui_icon plus-circle enabled"]')
        # Clicking the the button the number of times 
        for i in range(noa):
            add_adult_btn.click()
        for i in range(nos):
            add_senior_btn.click()
        for i in range(noc):
            add_child_btn.click()
    else:
        print('MORE THAN THE LIMIT')

Thanks.

CodePudding user response:

please try to use WebDriverWait with Expected Conditions

from selenium.webdriver.support import expected_conditions as EC
...
...
...

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH , '//span[@class = "ui_icon caret-down open-close"]'))).click()

Find more details here - https://selenium-python.readthedocs.io/waits.html

CodePudding user response:

To send the character sequence Bangalore within the From field on tripadvisor you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

driver.get('https://www.tripadvisor.com/CheapFlightsHome')
from_where = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='From where?']")))
from_where.click()
from_where.clear()
from_where.send_keys("Bangalore")
from_where = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='ui_typeahead_results']/ul/li"))).click()

Note: You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Browser Snapshot:

tripadvisor

  • Related