简体   繁体   English

IF CONDITION 在 Recaptcha 和另一个 Element 之间不起作用,只有 1 个有效。 Selenium python

[英]IF CONDITION doesn't work between Recaptcha and another Element, just 1 works. Selenium python

I am trying to do if-else condition for Selenium Python with Recaptcha.我正在尝试使用 Recaptcha 为 Selenium Python 做 if-else 条件。

I'm checking a website and sometimes a Recaptcha appears to solve.我正在检查一个网站,有时 Recaptcha 似乎可以解决问题。

and sometimes it doesn't appear and the submit button can be clicked.有时它不会出现,可以单击提交按钮。

I want the code to switch to Recaptcha and solve if it appears and click the submit button if not.我希望代码切换到 Recaptcha 并解决它是否出现,如果没有则单击提交按钮。

my tries我的尝试

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()

This method doesn't seem to work when Recaptcha doesn't pop up I'm guessing because the next portion of code is attempting to solve the Recaptcha and since it doesn't pop up it causes an error.当 Recaptcha 没有弹出时,此方法似乎不起作用我猜是因为下一部分代码试图解决 Recaptcha,并且由于它没有弹出,因此会导致错误。

The 2nd method I tried我试过的第二种方法

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

This method doesn't work if the Recaptcha doesn't appear.如果没有出现 Recaptcha,则此方法不起作用。

Any help please would be appreciated.如有任何帮助,我们将不胜感激。

Try like below once.像下面这样尝试一次。

Since its the Recaptcha that may or may not appear, attempt to click on the Recaptcha in the Try block.由于 Recaptcha 可能会出现也可能不会出现,因此请尝试单击 Try 块中的 Recaptcha。 And then click on the Submit button.然后单击“提交”按钮。

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.

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