selenium python selenuim中出现元素不可交互异常

hk8txs48  于 2023-01-26  发布在  Python
关注(0)|答案(2)|浏览(160)

所以我试着刮这一页:
https://www.tripadvisor.com/CheapFlightsHome
但是当我试图点击选择航班等级元素时,它只会给出这个错误:

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:

Python代码:

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')

谢谢。

izkcnapc

izkcnapc1#

请尝试在预期条件下使用WebDriverWait

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()

在此处查找更多详细信息-https://selenium-python.readthedocs.io/waits.html

kwvwclae

kwvwclae2#

要在tripadvisor上的***From*字段中发送 * 字符序列Bangalore *,需要为element_to_be_clickable()引入WebDriverWait,可以使用以下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()
    • 注意**:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

浏览器快照:

相关问题