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