简体   繁体   中英

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)

A couple things, Instead of using an If-else statement, use a Try-except statement. You should also not be using the "wait.until" command if you are unsure if an element will appear or not. If it does not appear, than it will wait indefinitely. See below code for changes, you may need to adjust it to your particular situation:

try:
    driver.find_element_by_xpath("//uni-view[@class='btn']").click() #button_one
    time.sleep(1)
    driver.find_element_by_xpath("//uni-view[@class='btn']").click() #button_two
    time.sleep(1)
    driver.find_element_by_xpath("//uni-view[@class='btn active']").click() #button_three
    time.sleep(1)
    driver.find_element_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() #button_four
    time.sleep(1)
except:
    driver.find_element_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() #button_four
    time.sleep(1)

To put this into words: the driver tries to find button_one, if it can then it continues to press button_two, button_three, then button_four. But if driver cannot find button_one, then it only presses button_four.

What if button 4 after except: command is also not available?script will through an error or just pass it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM