Home > Mobile >  Python, Selenium, webdriverwait until and timeexcepions
Python, Selenium, webdriverwait until and timeexcepions

Time:02-03

Hi I wrote a simple bot who paste ean code and searching a price. Everything works fine till seearching ean code which has no result on the page. Then bot stops program and show errors:

Traceback (most recent call last):
  File "C:\Python\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Python\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\Tarnowski\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\Tarnowski\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
    run()
  File "c:\Users\Tarnowski\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "C:\Python\lib\runpy.py", line 269, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Python\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Python\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Python\projects\main.py", line 53, in <module>
    ean.append(getdata(item))
  File "c:\Python\projects\main.py", line 29, in getdata
    price = WebDriverWait(driver,1).until(
  File "C:\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)

Anyone can help me fix this problem? I want to search products one by another using ean's code but when there are no such a code, skip this one or print "no result" and contiunue with next ean code.

eans = [
'5411183157583',
'5915081395316',
'5902431691770',
'3606489505943'
]
results = []

def getdata(symbol):
    PATH = ("chromedriver.exe ")
    driver.get(f"https://www.anyshop.com/result/?q={symbol}")
    
    price = WebDriverWait(driver,1).until(
        EC.presence_of_element_located((By.CLASS_NAME, "price-value "))
        ) 
    name = WebDriverWait(driver,1).until(
        EC.presence_of_element_located((By.CLASS_NAME, "item-product-name "))
        )

    records = {
      'symbol': symbol,
      'price': price.text,
      'name': name.text
    }
    print(symbol   ' '   price.text)
    return records

try:
  for item in eans:
    results.append(getdata(item))

except:
  print('no result')

CodePudding user response:

Just rearrange your try/except block to be contained within your for loop:

for item in eans:
    try:
        results.append(getdata(item))
    except:
        print('no result')

This way after you exit your except block, it will resume in the next iteration of your for loop.

I'd also recommend catching the specific exception TimeoutException, so if there's other errors, they won't get swallowed up accidentally. This just means importing the exception, and tacking that onto the except clause:

from selenium.common.exceptions import TimeoutException

try:
    ...
except TimeoutException:  # no other exceptions swallowed
    ...
  •  Tags:  
  • Related