0

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)

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for your help and fast reply, i tried that already but i keep getting this error IndentationError: expected an indented block after 'try' i did exactly how you explained but something is missing me, sorry it is a dumb problem
damm i forgot to align the code.. thats why it wasnt working.. fixed fast.. thanks and sorry for the previous dumb comment.. im still learning
No problems. So, now it works correct? Your problem is resolved?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.