简体   繁体   English

Selenium Python If-else 语句

[英]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 ^^我有一个小 python 脚本,它在休息 1 秒后按下一些不同的按钮(1、2、3 和 4) 一切正常,但有时只有按钮 4 出现在该网站上,而 1、2、3 没有,而我的脚本可以t 处理缺少 Button 1,2 和 3 的问题:( 我试图做一个 if-else 语句,但它不起作用。我也尝试了 try: and finally: 解决方案。也许你有一个小费给我我会很高兴^^


"Without if-else statement when every Button appears "每个Button出现时没有if-else语句

                  "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 “当仅出现按钮 4 时使用 if-else 语句。如果出现按钮 1,则 go 为 2、3 和 4。如果按钮 1 未出现,只需按按钮 4(所以我的想法 ^^)但它没有得到

    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.有几件事,不要使用 If-else 语句,而是使用Try-except语句。 You should also not be using the "wait.until" command if you are unsure if an element will appear or not.如果您不确定某个元素是否会出现,您也不应该使用“wait.until”命令。 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.换句话说:驱动程序试图找到 button_one,如果可以,则继续按 button_two、button_three,然后是 button_four。 But if driver cannot find button_one, then it only presses button_four.但是如果驱动程序找不到button_one,那么它只按下button_four。

What if button 4 after except: command is also not available?script will through an error or just pass it?如果在 except: 命令之后的按钮 4 也不可用怎么办?脚本将通过错误或只是通过它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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