Home > Mobile >  Python Selenium randomly fails to find element
Python Selenium randomly fails to find element

Time:01-26

My script is doing the same thing for every car in a list, but at a random passage

vec = WebDriverWait(driver, 60).until(
                        expected_conditions.visibility_of_element_located((
                            'id', 'elp.jobDetailsVehicle.input.jobVehicleEngineCode'))).get_attribute('value')

fails to find the element and i get this error:

  File "script.py", line 79, in check_recall
    WebDriverWait(driver, 60).until(
  File "C:\Users\Be26.LANGWEDEL\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
Backtrace:
    Ordinal0 [0x0125FDC3 2555331]
    Ordinal0 [0x011F77F1 2127857]
    Ordinal0 [0x010F2E08 1060360]
    Ordinal0 [0x0111E49E 1238174]
    Ordinal0 [0x0111E69B 1238683]
    Ordinal0 [0x01149252 1413714]
    Ordinal0 [0x01137B54 1342292]
    Ordinal0 [0x011475FA 1406458]
    Ordinal0 [0x01137976 1341814]
    Ordinal0 [0x011136B6 1193654]
    Ordinal0 [0x01114546 1197382]
    GetHandleVerifier [0x013F9622 1619522]
    GetHandleVerifier [0x014A882C 2336844]
    GetHandleVerifier [0x012F23E1 541697]
    GetHandleVerifier [0x012F1443 537699]
    Ordinal0 [0x011FD18E 2150798]
    Ordinal0 [0x01201518 2168088]
    Ordinal0 [0x01201660 2168416]
    Ordinal0 [0x0120B330 2208560]
    BaseThreadInitThunk [0x76C3FA29 25]
    RtlGetAppContainerNamedObjectPath [0x77057A9E 286]
    RtlGetAppContainerNamedObjectPath [0x77057A6E 238]

I already tried conditions like presence_of_element_located - same error. It's completely random. Any ideas?

EDIT: This is the entire loop:

for line in lines:
    if line[3] == '.':
        license_plate = line[23:33]
        name = line[38:52]
        vin = line[88:105]
        if '-' in license_plate and 'VSSZZZ' in vin:
            driver.switch_to.frame(driver.find_element('id', 'mainFs'))

            entry_license_plate = driver.find_element('id', 'elp.searchVehicle.input.licensePlate')
            entry_license_plate.send_keys(license_plate)

            driver.find_element('id', 'vaws.vehsearch.btn.search').click()

            vec = WebDriverWait(driver, 60).until(
                expected_conditions.visibility_of_element_located((
                    'id', 'elp.jobDetailsVehicle.input.jobVehicleEngineCode'))).get_attribute('value')

            WebDriverWait(driver, 60).until(
                expected_conditions.visibility_of_element_located(('id', 'vaws.sys.recall')))

            try:
                recall = driver.find_element('id', 'recall.info.state').get_attribute('src')
            except NoSuchElementException:
                recall = driver.find_element('id', 'recall.info.warning').get_attribute('src')

            if 'dummy.gif' in recall or 'info.gif' in recall:
                recall = 'Nein'
            elif 'warning.gif' in recall:
                recall = 'Ja'

            final_list  = f'{name} | {license_plate} | {vin} | Motor-Kennbuchstabe: {vec} ' \
                          f'| Feldmaßnahme: {recall}\n'

            print('done')

            driver.switch_to.default_content()

            WebDriverWait(driver, 60).until(
                expected_conditions.visibility_of_element_located(('id', 'toolbar.button.new.job'))).click()

The page is a in-house site from our company, so i'm not allowed to show it.

CodePudding user response:

This just means that the 60 second timeout ended without having observed what is in the until method.

Try to put this inside of a try :

try:
    vec = WebDriverWait(driver, 60).until(
                        expected_conditions.visibility_of_element_located((
                            'id', 'elp.jobDetailsVehicle.input.jobVehicleEngineCode'))).get_attribute('value')
except:
    print("Timeout")

Or, to try again until we got no error :

while True:
    try:
        vec = WebDriverWait(driver, 60).until(
                        expected_conditions.visibility_of_element_located((
                            'id', 'elp.jobDetailsVehicle.input.jobVehicleEngineCode'))).get_attribute('value')
        # break out of the loop if successful
        break
    except:
        # continue the loop if not successful
        continue
    print("Timeout")

CodePudding user response:

Ok, i think i found the problem: at the beginning i load the default profile with options.add_argument(f'--user-data-dir={os.getenv("LOCALAPPDATA")}\\Google\\Chrome\\User Data' Without that i don't have these random errors. Does anyone know why?

  •  Tags:  
  • Related