Home > Blockchain >  Can't Find Element Inside iframe
Can't Find Element Inside iframe

Time:08-14

I want to get the data-sitekey, but it is inside the iframe.

I only can get the element in , can't find the element insde of it.

How can I get the data-sitekey? HTML

driver.get(url)
driver.switch_to.frame("main-iframe")

container= driver.find_element(By.CLASS_NAME, 'container')
print(container)

time.sleep(2)

captcha = driver.find_element(By.CLASS_NAME, 'g-recaptcha')
print(captcha)

CodePudding user response:

The reCAPTCHA element is within an <iframe>

reCAPTCHA_frame


Solution

To extract the value of the data-sitekey attribute you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the visibility_of_element_located.

  • You can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#main-iframe")))
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.g-recaptcha"))).get_attribute("data-sitekey"))
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='main-iframe']"))).get_attribute("data-sitekey"))
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='g-recaptcha']")))
      
  • 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
    

CodePudding user response:

This is how you get that information:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t

firefox_options = Firefox_Options()

# firefox_options.add_argument("--width=1280")
# firefox_options.add_argument("--height=720")
# firefox_options.headless = True

driverService = Service('chromedriver/geckodriver')

browser = webdriver.Firefox(service=driverService, options=firefox_options)

url = 'https://premier.hkticketing.com/'
browser.get(url) 
t.sleep(5)
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[@id='main-iframe']")))
print('switched')
t.sleep(5)
element_x = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='g-recaptcha']"))  )
print(element_x.get_attribute('data-sitekey'))

Result printed in terminal:

switched
6Ld38BkUAAAAAPATwit3FXvga1PI6iVTb6zgXw62

Setup is for linux/Firefox/geckodriver, but you can adapt it to your own system, just mind the imports, and the code after defining the browser.

Selenium docs: https://www.selenium.dev/documentation/

  • Related