繁体   English   中英

Selenium Python If-else 语句

[英]Selenium Python If-else Statement

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


"每个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) 

“当仅出现按钮 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)

有几件事,不要使用 If-else 语句,而是使用Try-except语句。 如果您不确定某个元素是否会出现,您也不应该使用“wait.until”命令。 如果它没有出现,那么它将无限期地等待。 请参阅下面的代码进行更改,您可能需要根据您的特定情况对其进行调整:

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)

换句话说:驱动程序试图找到 button_one,如果可以,则继续按 button_two、button_three,然后是 button_four。 但是如果驱动程序找不到button_one,那么它只按下button_four。

如果在 except: 命令之后的按钮 4 也不可用怎么办?脚本将通过错误或只是通过它?

暂无
暂无

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

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