0

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

I only can get the element in class="container", 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)
Leung
  • 23
  • 2

2 Answers2

0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    sorry i don't understand how can i get it... I have already get in iframe `driver.switch_to.frame("main-iframe")` – Leung Aug 13 '22 at 14:59
  • Checkout the updated answer and let me know the status – undetected Selenium Aug 13 '22 at 15:15
  • [21884:21888:0813/231822.516:ERROR:gpu_init.cc(486)] Passthrough is not supported, GL is disabled, ANGLE is Traceback (most recent call last): File "e:\repo\concert\concert.py", line 136, in element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='container']//div[@class='g-recaptcha']"))) File "C:\Users\leung\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until raise TimeoutException(message, screen, stacktrace) – Leung Aug 13 '22 at 15:20
  • Checkout the updated answer and let me know the status. – undetected Selenium Aug 13 '22 at 15:26
  • [21020:13200:0813/232742.194:ERROR:gpu_init.cc(486)] Passthrough is not supported, GL is disabled, ANGLE is Traceback (most recent call last): File "e:\repo\concert\concert.py", line 136, in element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='g-recaptcha']"))) File "C:\Users\leung\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace: – Leung Aug 13 '22 at 15:28
  • Let's discuss the issue in Selenium Room. – undetected Selenium Aug 13 '22 at 15:31
  • or i give you the url to try? https://premier.hkticketing.com/ – Leung Aug 13 '22 at 15:31
  • I don't find any `iframe` even within the [website](https://premier.hkticketing.com/) – undetected Selenium Aug 13 '22 at 15:33
  • I just find you in Selenium Room – Leung Aug 13 '22 at 15:34
0

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/

Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30