【问题标题】:Put Multiple Functions into one Switch-Case Statement - Python将多个函数放入一个 Switch-Case 语句 - Python
【发布时间】:2021-07-30 06:37:28
【问题描述】:

我的脚本目前使用三个不同的函数来查找四种不同的场景。这样做有时会导致错误,因为我不知道哪个场景会首先发生,或者它是否会发生。我认为将这些场景放在一个“switch-case”语句中可以解决这个问题。

下面是三个函数:

def bypass_consent(driver):
    try:
        consent = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
            (By.XPATH, "//input[@type='submit' and @value='I agree']")))
        consent.submit()
    except:
        try:
            consent = driver.find_element_by_css_selector(
                'button.tftvyvi6t678')
            consent.click()
        except:
            pass

def bypass_agree(driver):
    frame = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
        (By.ID, "iframe")))
    driver._switch_to.frame(frame)
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
        (By.ID, "introAgreeButton"))).click()
    driver.switch_to.default_content()

def bypass_signin(driver):
    sleep(1)
    nothanks = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
        (By.CLASS_NAME, "style-scope.fu-button-renderer.style-text.size-small")))
    nothanks.click()
    sleep(randint(1, 5))
    bypass_agree(driver)

这是我尝试过的(不成功):

def scenario(i):
    switcher={
            0:consent = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                        (By.XPATH, "//input[@type='submit' and @value='I agree']")))
                        consent.submit(),
            1:consent = driver.find_element_by_css_selector(
                        'button.tftvyvi6t678')
                        consent.click(),
            2:frame = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                      (By.ID, "iframe")))
                      driver._switch_to.frame(frame)
                      WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                      (By.ID, "introAgreeButton"))).click()
                      driver.switch_to.default_content(),
            3:sleep(1)
              nothanks = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                         (By.CLASS_NAME, "style-scope.fu-button-renderer.style-text.size- 
                         small")))
                         nothanks.click()
                         sleep(randint(1, 5))
                         bypass_agree(driver)
            
         }
     return switcher.get(i)

【问题讨论】:

  • 怎么不成功了?意外的输出(预期的结果)?错误?
  • 我得到一个SyntaxError: invalid syntax 指向0:consent = WebDriverWait(driver, 30).until(EC.element_to_be_clickable( 中的等号

标签: python switch-statement


【解决方案1】:

你的代码字典可以这样修复:

def case_0():
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                    (By.XPATH, "//input[@type='submit' and @value='I agree']")))
                    consent.submit()

switcher={
    0: case_0

    # ...
}

switcher[case]()

或者您可以使用 exec()(可能不使用)

exec('WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='submit' and @value='I agree']"))); consent.submit()')

我知道类似 switch 语句的东西将在 3.10 中实现...

【讨论】:

  • 如何解决不同的场景?
【解决方案2】:

你可以这样写一个开关函数:

def switch(v): yield lambda *c: v in c

在 for 语句中使用,使用类似 C 的模式:

for case in switch(i):
    if case(0):
        consent = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                    (By.XPATH, "//input[@type='submit' and @value='I agree']")))
        consent.submit()
        break

    if case(1):
        consent = driver.find_element_by_css_selector(
                    'button.tftvyvi6t678')
        consent.click()
        break

    if case(2):
        frame = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                  (By.ID, "iframe")))
        driver._switch_to.frame(frame)
        WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                  (By.ID, "introAgreeButton"))).click()
        driver.switch_to.default_content()
        break

    if case(3):
        sleep(1)
        nothanks = WebDriverWait(driver, 30).until(EC.element_to_be_clickable(
                     (By.CLASS_NAME, "style-scope.fu-button-renderer.style-text.size- 
                     small")))
        nothanks.click()
        sleep(randint(1, 5))
        bypass_agree(driver)
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2013-05-18
    • 2020-03-15
    相关资源
    最近更新 更多