[英]Put Multiple Functions into one Switch-Case Statement - Python
My script currently looks for four different scenarios by using three different functions.我的脚本目前通过使用三个不同的函数来查找四种不同的场景。 Doing it this way sometimes causes errors because I don't know which scenario will occur first or if it will occur at all.
这样做有时会导致错误,因为我不知道哪个场景会首先发生,或者它是否会发生。 I think that putting these scenarios into one "switch-case" statement would fix this problem.
我认为将这些场景放入一个“switch-case”语句中可以解决这个问题。
Here are the three functions:以下是三个功能:
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)
Here's what I tried (unsuccesssfully):这是我尝试过的(不成功):
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)
Your dictionary of code can be fixed with this:您的代码字典可以这样修复:
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]()
or you could use an exec() (probably don't)或者你可以使用 exec() (可能不要)
exec('WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='submit' and @value='I agree']"))); consent.submit()')
I know that something like a switch statement is going to be implemented in 3.10 though...我知道类似 switch 语句之类的东西将在 3.10 中实现......
You can write a switch function like this:你可以这样写一个开关 function :
def switch(v): yield lambda *c: v in c
It is used in a for statement, using a C-like pattern:它用于 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.