I'm working on a bot for the game 'Crazy Time' on various sites. Using Selenium with Python, I can successfully connect to the website and switch to the desired iframe. However, I'm unable to detect any elements inside this iframe.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException
import logging
# Set up logging
logging.basicConfig(filename='bot.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('Script started')
def is_browser_connected(driver):
try:
driver.current_url
return True
except Exception as e:
logging.error("Error checking browser connection: %s", e)
return False
def iframe_detected(driver):
iframe_selector = "#game"
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, iframe_selector)))
return True
except (NoSuchElementException, TimeoutException) as e:
logging.error("Error detecting iframe: %s", e)
return False
def switch_to_game_iframe(driver):
iframe_selector = "#game"
try:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, iframe_selector)))
except Exception as e:
logging.error("Error switching to iframe: %s", e)
def check_bet_element_presence(driver):
bet_element_selector = "#root > div > div.app--2c5f6 > div > div.content--82383 > div.footerWrapper--3a742 > div > div.perspectiveContainer--20de4 > div.bettingGrid--fbfc0 > div > div > div > div.betSpotsContainer--27079.isLandscape--7c6c3 > div:nth-child(1)"
try:
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, bet_element_selector)))
print("Element is found!")
return True
except (NoSuchElementException, TimeoutException) as e:
logging.error("Error checking bet element presence: %s", e)
print("Element not found!")
return False
def start_browser():
# Set up browser
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
user_data_dir = r'C:\Users\ciezz\AppData\Local\Google\Chrome\User Data'
options.add_argument(f"--user-data-dir={user_data_dir}")
options.binary_location = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get("https://stake.com/casino/games/evolution-crazy-time")
# Check browser connection
input("Press enter to see if we are still connected to the browser...")
if is_browser_connected(driver):
print("Connection is detected!")
else:
print("Not connected to the browser. Try again.")
# Check game detection
input("Press enter to switch to game iframe and detect...")
if iframe_detected(driver):
print("The program is connected to the game!")
else:
print("The game is not detected. Try again.")
# Switch to game iframe
switch_to_game_iframe(driver)
# Check bet element presence without placing bet
input("Press enter to check bet element presence...")
check_bet_element_presence(driver)
driver.quit()
start_browser()
My selectors are consistent across sessions (non-dynamic). There don't seem to be nested iframes or issues with the DOM that I can detect. Chrome version: 115.0.5790.171 Chromedriver version: 115.0.5790.170 (latest available for Windows).
Actually now I see that the elements that are viewable in the devtools (inspect mode) are not inside the source code of the iframe at all. Would this mean that the elements are dynamic ? Do they appear only by JavaScript or something ?
And here are some logs
2023-08-08 04:47:52,399 - DEBUG - Finished Request 2023-08-08 04:47:52,900 - DEBUG - POST http://localhost:49173/session/ea6084a66bd5d613c38dbec01bdd15fc/element {"using": "css selector", "value": "#root > div > div.app--2c5f6 > div > div.content--82383 > div.footerWrapper--3a742 > div > div.perspectiveContainer--20de4 > div.bettingGrid--fbfc0 > div > div > div > div.betSpotsContainer--27079.isLandscape--7c6c3 > div:nth-child(1)"} 2023-08-08 04:47:52,905 - DEBUG - http://localhost:49173 "POST /session/ea6084a66bd5d613c38dbec01bdd15fc/element HTTP/1.1" 404 0 2023-08-08 04:47:52,905 - DEBUG - Remote response: status=404 | data={"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\":\"css selector\",\"selector\":\"#root > div > div.app--2c5f6 > div > div.content--82383 > div.footerWrapper--3a742 > div > div.perspectiveContainer--20de4 > div.bettingGrid--fbfc0 > div > div > div > div.betSpotsContainer--27079.isLandscape--7c6c3 > div:nth-child(1)\"}\n (Session info: chrome=115.0.5790.171)","stacktrace":"Backtrace:\n\tGetHandleVerifier [0x00007FF75B914A62+57106]\n\t(No symbol) [0x00007FF75B88CF52]\n\t(No symbol) [0x00007FF75B75E2CB]\n\t(No symbol) [0x00007FF75B79786E]\n\t(No symbol) [0x00007FF75B79795C]\n\t(No symbol) [0x00007FF75B7D0477]\n\t(No symbol) [0x00007FF75B7B69FF]\n\t(No symbol) [0x00007FF75B7CE522]\n\t(No symbol) [0x00007FF75B7B6793]\n\t(No symbol) [0x00007FF75B78CE81]\n\t(No symbol) [0x00007FF75B78E064]\n\tGetHandleVerifier [0x00007FF75BBC4222+2873042]\n\tGetHandleVerifier [0x00007FF75BC16590+3209792]\n\tGetHandleVerifier [0x00007FF75BC0F3AF+3180639]\n\tGetHandleVerifier [0x00007FF75B9A5F25+652245]\n\t(No symbol) [0x00007FF75B898618]\n\t(No symbol) [0x00007FF75B8947C4]\n\t(No symbol) [0x00007FF75B8948BC]\n\t(No symbol) [0x00007FF75B884C33]\n\tBaseThreadInitThunk [0x00007FFBDEC126AD+29]\n\tRtlUserThreadStart [0x00007FFBE00AAA68+40]\n"}} | headers=HTTPHeaderDict({'Content-Length': '1287', 'Content-Type': 'application/json; charset=utf-8', 'cache-control': 'no-cache'}) 2023-08-08 04:47:52,905 - DEBUG - Finished Request 2023-08-08 04:47:53,406 - DEBUG - POST http://localhost:49173/session/ea6084a66bd5d613c38dbec01bdd15fc/element {"using": "css selector", "value": "#root > div > div.app--2c5f6 > div > div.content--82383 > div.footerWrapper--3a742 > div > div.perspectiveContainer--20de4 > div.bettingGrid--fbfc0 > div > div > div > div.betSpotsContainer--27079.isLandscape--7c6c3 > div:nth-child(1)"} 2023-08-08 04:47:53,412 - DEBUG - http://localhost:49173 "POST /session/ea6084a66bd5d613c38dbec01bdd15fc/element HTTP/1.1" 404 0 2023-08-08 04:47:53,412 - DEBUG - Remote response: status=404 | data={"value":{"error":"no such element","message":"no such element: Unable to locate element: {\"method\":\"css selector\",\"selector\":\"#root > div > div.app--2c5f6 > div > div.content--82383 > div.footerWrapper--3a742 > div > div.perspectiveContainer--20de4 > div.bettingGrid--fbfc0 > div > div > div > div.betSpotsContainer--27079.isLandscape--7c6c3 > div:nth-child(1)\"}\n (Session info: chrome=115.0.5790.171)","stacktrace":"Backtrace:\n\tGetHandleVerifier [0x00007FF75B914A62+57106]\n\t(No symbol) [0x00007FF75B88CF52]\n\t(No symbol) [0x00007FF75B75E2CB]\n\t(No symbol) [0x00007FF75B79786E]\n\t(No symbol) [0x00007FF75B79795C]\n\t(No symbol) [0x00007FF75B7D0477]\n\t(No symbol) [0x00007FF75B7B69FF]\n\t(No symbol) [0x00007FF75B7CE522]\n\t(No symbol) [0x00007FF75B7B6793]\n\t(No symbol) [0x00007FF75B78CE81]\n\t(No symbol) [0x00007FF75B78E064]\n\tGetHandleVerifier [0x00007FF75BBC4222+2873042]\n\tGetHandleVerifier [0x00007FF75BC16590+3209792]\n\tGetHandleVerifier [0x00007FF75BC0F3AF+3180639]\n\tGetHandleVerifier [0x00007FF75B9A5F25+652245]\n\t(No symbol) [0x00007FF75B898618]\n\t(No symbol) [0x00007FF75B8947C4]\n\t(No symbol) [0x00007FF75B8948BC]\n\t(No symbol) [0x00007FF75B884C33]\n\tBaseThreadInitThunk [0x00007FFBDEC126AD+29]\n\tRtlUserThreadStart [0x00007FFBE00AAA68+40]\n"}} | headers=HTTPHeaderDict({'Content-Length': '1287', 'Content-Type': 'application/json; charset=utf-8', 'cache-control': 'no-cache'}) 2023-08-08 04:47:53,412 - DEBUG - Finished Request