IF CONDITION 在 Recaptcha 和另一个 Element 之间不起作用,只有 1 个有效。硒 python
IF CONDITION doesn't work between Recaptcha and another Element, just 1 works. Selenium python
我正在尝试使用 Recaptcha 为 Selenium Python 做 if-else 条件。
我正在查看一个网站,有时 Recaptcha 似乎可以解决问题。
有时不出现,可以点击提交按钮。
我希望代码切换到Recaptcha并解决是否出现,如果没有则单击提交按钮。
我的尝试
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='recaptcha-anchor']"))).click()
driver.switch_to.default_content()
try:
driver.find_element(By.ID,"btnSSSubmit").click()
except:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge expires in two minutes']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-"))).click()
当 Recaptcha 没有弹出时,此方法似乎不起作用我猜是因为下一部分代码试图解决 Recaptcha,但由于它没有弹出,因此会导致错误。
我试过的第二种方法
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='recaptcha-anchor']"))).click()
driver.switch_to.default_content()
try:
if(len(driver.find_elements(By.XPATH, "btnSSSubmit"))) > 0 :
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSSSubmit"))).click()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge expires in two minutes']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha"))).click()
except:
pass
如果没有出现 Recaptcha,则此方法无效。
如有任何帮助,我们将不胜感激。
像下面这样尝试一次。
由于 Recaptcha 可能会出现也可能不会出现,请尝试单击 Try 块中的 Recaptcha。然后点击提交按钮。
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
try:
# Try to solve the Recaptcha.
except:
print("Recaptcha did not appear")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSSSubmit"))).click()
我正在尝试使用 Recaptcha 为 Selenium Python 做 if-else 条件。
我正在查看一个网站,有时 Recaptcha 似乎可以解决问题。
有时不出现,可以点击提交按钮。
我希望代码切换到Recaptcha并解决是否出现,如果没有则单击提交按钮。
我的尝试
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='recaptcha-anchor']"))).click()
driver.switch_to.default_content()
try:
driver.find_element(By.ID,"btnSSSubmit").click()
except:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge expires in two minutes']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-"))).click()
当 Recaptcha 没有弹出时,此方法似乎不起作用我猜是因为下一部分代码试图解决 Recaptcha,但由于它没有弹出,因此会导致错误。
我试过的第二种方法
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='recaptcha-anchor']"))).click()
driver.switch_to.default_content()
try:
if(len(driver.find_elements(By.XPATH, "btnSSSubmit"))) > 0 :
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSSSubmit"))).click()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge expires in two minutes']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha"))).click()
except:
pass
如果没有出现 Recaptcha,则此方法无效。
如有任何帮助,我们将不胜感激。
像下面这样尝试一次。
由于 Recaptcha 可能会出现也可能不会出现,请尝试单击 Try 块中的 Recaptcha。然后点击提交按钮。
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
try:
# Try to solve the Recaptcha.
except:
print("Recaptcha did not appear")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSSSubmit"))).click()