I'm trying to automate a task on https://app.gala.games/games/town-star/play. The problem is I can't find any elements. I already tried to find the element with Xpath, Id, Class, Css but I keep getting this error: line 1244, in find_element return self.execute(Command.FIND_ELEMENT, { . The def in the code below is to connect to a already existing session (this part works I can switch sites with the driver). The problem can't be that the element isn't loaded but I still tried to have a big delay between finding the element and connecting to the driver. I hope someone knows how to solve this.
from selenium.webdriver.common.by import By
import json
from time import sleep
def create_driver_session(session_id, executor_url):
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
# Save the original function, so we can revert our patch
org_command_execute = RemoteWebDriver.execute
def new_command_execute(self, command, params=None):
if command == "newSession":
# Mock the response
return {'success': 0, 'value': None, 'sessionId': session_id}
else:
return org_command_execute(self, command, params)
# Patch the function before creating the driver object
RemoteWebDriver.execute = new_command_execute
new_driver = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
new_driver.session_id = session_id
# Replace the patched function with original function
RemoteWebDriver.execute = org_command_execute
return new_driver
# Load session info
with open("session.json", 'r') as f:
jsonload = json.load(f)
session_id = jsonload[1]
executor_url = jsonload[0]
print(session_id, executor_url)
# Create driver of existing tab
driver = create_driver_session(session_id, executor_url)
# pause
sleep(2)
# Process
yes = driver.find_element(By.CLASS_NAME, 'yes')
yes.click()```
CodePudding user response:
<iframe data-v-5b122db5="" src="https://townstar.sandbox-games.com/launch#eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoidGVtcG9yYXJ5IiwiaWQiOiI2MWQ5Njk4YjY4YzAyODY0MTJmZmYzMjEiLCJyZWZlcnJhbExpbmsiOiJodHRwczovL2dhbGEuZmFuL1otRmRNSElMaSIsImlhdCI6MTY0MTYzODI4MywiZXhwIjoxNjQyMjQzMDgzLCJhdWQiOiJ1cm46Z2FsYUFwaSIsImlzcyI6InVybjpnYWxhQXBpIiwic3ViIjoiZ2FsYUFwaTpzdWJqZWN0In0.imC13qJAT_tBJt8ej2XGi2e4KMQdZo-sGxAg4BbPmRdvon5lESVVMGsT0XSbaCGCkHCc8iRpw1lLdYcTH3UqKedkfdXxBAFzoOVbvX6Tl5QnS_iDJD8pN_FlyClNlwVcEW9Top8Q3tW4l77JIghi8s26f1rRH__RmMudOKNpxopeDOe7kdlDhkxLMm97YbN2c6deTznp3_dMvWQMWw-gKKOZj_a1OZGMQqAoSFkG-PVmYWzdjtY2WdGVDU8qhi7NH7Wn1IJiIcWFaG9W9fBpSGv2gMERfMbPyH59VvN6qYoGLJacO-Ptmwa4tJte_bdhA5WR7cjxnLjS84QQBfArhGSIR6X0oRPacCcO63A7rjp12KxExZFwUrkdbzoWHDcyXpedyXgBGHCu9U1Ry8JxtDhATeYxQUNJkzlPV42T4vZ6Mchlpr8wAYJs4E7y6A019CGl_12wTJaDKo4MgAnDnMdUdsnZgZEQMRJ39DIMu0oU7P024BKy9DP-vlf8G2-Nk3P_XP6s5tCW3x_WTLhwEPY-DEHA_um2oXWVgD2zqaJcsjNlgyxiE0Wfcsq-azIT9Ao2FR9YG2NgCUKoitT-3s-4eSiKuBWHfUbyRZxVTRQ7gJ68ZHiy14JrZs5bcShKsdJ_8ZERqGDP4C0lRsec3yROl2Ia88xqyUGxzusG5eQ"></iframe>
Your element is in an iframe please switch to it prior to finding elements.
driver.switch_to.frame(driver.find_element_by_css_selector("iframe"))
