Partage
  • Partager sur Facebook
  • Partager sur Twitter

si la variable existe (plus compliquer)

Sujet résolu
    26 janvier 2023 à 13:30:57

    Je suis actuellement en train de travailler avec Selenium et je rencontre un problème lors de la déclaration d'une variable. Le problème est que si l'élément recherché pour remplir la variable n'existe pas, je souhaiterais simplement passer à autre chose plutôt qu'une erreur.

    case = driver.find_element(
        "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/table/tbody/tr[2]/td[2]/span")
    case.click()
    
    case = driver.find_element(
        "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/table/tbody/tr[3]/td[2]/span")
    case.click()
    
    case = driver.find_element(
        "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/table/tbody/tr[4]/td[3]/span")
    case.click()
    
    case = driver.find_element(
        "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/table/tbody/tr[5]/td[1]/span")
    case.click()
    
    case = driver.find_element(
        "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/table/tbody/tr[6]/td[1]/span")
    case.click()
    
    case = driver.find_element(
        "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/table/tbody/tr[7]/td[2]/span")
    case.click()
    
    boutton = driver.find_element(
        "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/div[3]/input")
    boutton.click()
    
     

    j ai meme esseyer :

    if(driver.find_element(
        "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/div[3]/input")) : 
        boutton = driver.find_element(
            "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/div[3]/input")
    
     

    merci d avoir lu jusque la 

    -
    Edité par NÉKOgrile 26 janvier 2023 à 13:33:33

    • Partager sur Facebook
    • Partager sur Twitter
      26 janvier 2023 à 14:11:09

      try:
          case = driver.find_element(
          "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/table/tbody/tr[2]/td[2]/span")
          case.click()
      except NoSuchElementException:
          ...

      -
      Edité par josmiley 26 janvier 2023 à 14:11:25

      • Partager sur Facebook
      • Partager sur Twitter

      Python c'est bon, mangez-en. 

        26 janvier 2023 à 19:12:35

        A mon avis, les erreurs se produisent sur les click et pas sur les find_element, le find_element va retourner None si il ne trouve pas l'élément en question; donc le test se fera après 

        boutton = driver.find_element(
            "xpath", "/html/body/div[1]/div[3]/div[2]/form/div/div[3]/input")
        if(button):
            boutton.click()
        



        • Partager sur Facebook
        • Partager sur Twitter

        si la variable existe (plus compliquer)

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown