lundi 29 novembre 2021

Selenium Python If-else Statement

i have a little python script which presses some different buttons after 1 sec break (1,2,3 and 4) Everything is working fine but sometimes only Button 4 appears on that website and 1,2,3 not, and my script can't handle that Button 1,2 and 3 are missing :( I tried to make a if-else statement but its not working. I also tryed the try: and finally: solution. Maybe you have a tipp for me I would be very happy ^^


"Without if-else statement when every Button appears

                  "Button 1 appears
    wait.until(EC.element_to_be_clickable((By.XPATH, "//uni-view[@class='btn']"))).click()
    time.sleep(1) "Button 2 appears
    wait.until(EC.element_to_be_clickable((By.XPATH, "//uni-view[@class='btn']"))).click()
    time.sleep(1) "Button 3 appears
    wait.until(EC.element_to_be_clickable((By.XPATH, "//uni-view[@class='btn active']"))).click()
    time.sleep(1) "Button 4 appears
    wait.until(EC.element_to_be_clickable((By.XPATH,"/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-movable-area/uni-scroll-view/div/div/div/uni-view/uni-view[3]/uni-view[2]/uni-view/uni-view/img[2]"))).click()
    time.sleep(1) 

"With if-else statement when only Button 4 appears. If Button 1 appears, go to 2, 3, and 4. If Button 1 not appears just press Button 4 (so my idea ^^) But it doesn't get

    if driver.find_element_by_xpath("//uni-view[@class='btn']"):
        wait.until(EC.element_to_be_clickable((By.XPATH, "//uni-view[@class='btn']"))).click()
        time.sleep(1)
        wait.until(EC.element_to_be_clickable((By.XPATH, "//uni-view[@class='btn']"))).click()
        time.sleep(1)
        wait.until(EC.element_to_be_clickable((By.XPATH, "//uni-view[@class='btn active']"))).click()
        time.sleep(1)
        wait.until(EC.element_to_be_clickable((By.XPATH,"/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-movable-area/uni-scroll-view/div/div/div/uni-view/uni-view[3]/uni-view[2]/uni-view/uni-view/img[2]"))).click()
        time.sleep(1)
    else:
        wait.until(EC.element_to_be_clickable((By.XPATH,"/html/body/uni-app/uni-page/uni-page-wrapper/uni-page-body/uni-view/uni-movable-area/uni-scroll-view/div/div/div/uni-view/uni-view[3]/uni-view[2]/uni-view/uni-view/img[2]"))).click()
        time.sleep(1)

Aucun commentaire:

Enregistrer un commentaire