Home > Software design >  Make Python continue if doesnt find html element
Make Python continue if doesnt find html element

Time:10-13

im new to Python, and everything that i know i learned by myself so i have a huge gap in some things like language structure, it would be awsome if someone could help me.

Im building something with selenium and openpyxl and i face a problem, if it doesnt find an element the code stops there, and thats exactly what im trying to bypass without any success. I need to continue if the element not found.

Heres a piece of my currently code, can someone help me how can i do that?

Thanks

#distin_city varia
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[11]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/div[1]/span[1]"))).click()
element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[11]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/input[1]")))
element.send_keys(destin_city[:3])
time.sleep(0.2)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[11]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/ul[1]/li[1]/div[3]/span[1]/div[1]"))).click()
print("DESTINATION CITY - ",destin_city)

#f_payment varia    
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[12]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/div[1]/span[1]/span[1]"))).click()
element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[12]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/input[1]")))
element.send_keys(f_payment[:3])
time.sleep(0.2)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[12]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/ul[1]/li[1]/div[3]/span[1]/div[1]"))).click()
print("FREIGHT PAYMENT TYPE - ",f_payment)

#currency varia    
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[13]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/div[1]/span[1]"))).click()
element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[13]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/input[1]")))
element.send_keys(currency[:1])
time.sleep(0.2)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[13]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/ul[1]/li[1]/div[3]/span[1]/div[1]"))).click()
print("CURRENCY - ",currency)

CodePudding user response:

In order to simply continue with the flow in case the element is not found you can use try-except block with pass in except section.
For example if you want to apply that on

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[11]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/div[1]/span[1]"))).click()

You can do this:

try:
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html[1]/body[1]/div[1]/div[1]/div[1]/div[2]/form[1]/ivd-form-field[11]/div[1]/div[1]/ivd-field[1]/div[1]/ivd-select-box-field[1]/div[1]/div[1]/div[1]/span[1]"))).click()
except Exception:
    pass

CodePudding user response:

What you're looking for is try, except. Say you want one or multiple lines to be checked. If anything failed there you can do something else (Or in your example do nothing and pass)

You wrap your line(s) with try, except.

try:
   # This is your code
   Do Something 1
   Do something 2
except:
   # This is your code when any line above fails. You can use `pass` to do nothing
   Do something else
  • Related