如果未找到 html 元素,则使 Python 继续

Make Python continue if doesnt find html element

我是 Python 的新手,我所知道的一切都是自学的,所以我在语言结构等方面存在巨大差距,如果有人能帮助我,那就太棒了。

我正在用 selenium 和 openpyxl 构建一些东西,我遇到了一个问题,如果它没有找到一个元素,代码就停在那里,而这正是我试图绕过但没有成功的原因。如果找不到元素,我需要继续。

这是我目前的一段代码,有人可以帮我吗?

谢谢

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

为了在找不到元素的情况下简单地继续流程,您可以在 except 部分使用 try-except 块和 pass
例如,如果你想将其应用于

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

你可以这样做:

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

您要查找的是 tryexcept。假设您要检查一行或多行。如果那里有任何失败,你可以做其他事情(或者在你的例子中什么都不做 pass

您用 tryexcept.

换行
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