1

I switched to a frame inside a frame with selenium, and I accessed to that frame, but I'm unable to find elements inside the frame using find_element_by_tag_name or find_element_by_xpath, I'm sure the element exists, but I cannot find it and click on it with selenium.

page > frame1(elements: checkbox) > frame2(elements: imgs , buttons)

There are buttons in frame2 I want to click on it, and frame2 shown after checkbox clicked. Here is my code:

driver = webdriver.Firefox(r"C:\Users\[profilename]\Desktop\devloper\selenium\gecko")
driver.get(url)

sleep(10) #to make sure that everything loaded

#the first frame (success to access and clicked):
driver.switch_to.frame(0) 
click1 = driver.find_element_by_xpath("//*[@id=\"anchor\"]")
click1.click()

sleep(5) #to make sure that everything loaded

iframe = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)

sleep(3)

frame2_element = driver.find_element_by_tag_name("button")
frame2_element.click()

Error msg:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element

I used find_element_by_tag_name and before that i tried XPATH and it had same error.

I tried hard to explain everything i'm not good with explaining things to people so i hope u understand me, and please tell me what to do :( .

The url is : reCAPTCHA demo

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jawad
  • 186
  • 1
  • 14

2 Answers2

1

The checkbox element with in the reCAPTCHA demo page is within an <iframe> so to invoke click() on the checkbox you have to:

  • Induce WebDriverWait for the desired frame_to_be_available_and_switch_to_it().

Next, to click on the Submit button:


Solution

You can use the following based Locator Strategies:

  • Code Block:

    driver.get("https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox-explicit.php")
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://www.google.com/recaptcha/api2/anchor')]")))
    WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='recaptcha-checkbox-spinner-overlay']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='recaptcha-checkbox-border']"))).click()
    driver.switch_to.default_content()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='form-field']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

References

You can find a couple of relevant discussions in:


Update

As per your recent comment update this element that i want to click on //*[@id="recaptcha-audio-button"] to click on the reCAPTCHA audio button you can use the following Locator Strategies:

driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()

You can find a relevant detailed discussion in How to click on elements within an iframe to enable the captcha through the audio using Selenium and Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thx for trying to help me out, but it didn't work, i edited your code to make it work but still. i think you understood my idea wrong, i meant by frame2 this frame: [link](https://i.ibb.co/5jbdLwR/Screenshot-2020-05-25-re-CAPTCHA-demo-I-m-not-a-robot-checkbox-Explicit-render.png). as i saw reCaptcha has two frames the first one is checkbox ease to click, but frame2 is the issue. if u did understand my idea, i'm sorry but it didnt work and i got two errors the first: from the website: `Cannot contact reCAPTCHA. Check your connection and try again.` this came when the recaptcha passed – Jawad May 25 '20 at 07:57
  • the second error : `selenium.common.exceptions.ElementClickInterceptedException: Message: Element – Jawad May 25 '20 at 07:59
  • this element that i want to click on //*[@id="recaptcha-audio-button"]. – Jawad May 25 '20 at 08:06
  • @Jawad Added the solution to click on the **recaptcha-audio-button**. Check out the answer update and let me know the status. – undetected Selenium May 25 '20 at 10:10
  • 1
    it works thank you, i have been trying for days to do it i feel like i'm newbie. thank you – Jawad May 25 '20 at 11:00
0

Since you haven't posted the url, we don't know if the element exists or not, but if yes, you can try wait for it to load. It's explained here

  • as u saw i added sleep and i used wait.until but same error the url is : https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox-explicit.php , frame 1 is reCaptcher – Jawad May 24 '20 at 08:41