I am trying to access the below iframe:
But everything I try it is unable to locate it, therefore the first_name_value element cannot be found to interact with and populate the text box.
Two avenues included in the code below, one is accessing normally with xpath and the other is hard defining of the iframe name and using js. Neither work.
Here is the HTML for the iframe, no src may be the issue? If so how can I overcome this
<iframe tabindex="-1" id="swift-registration-306700405343797-2" name="swift-registration-306700405343797-2" title="swift-registration-306700405343797-2" allow="screen-wake-lock" src="about:blank" style="width: 100%; height: 684px; position: relative; top: 0px; right: 0px; z-index: 10;"> </iframe>
This is the code
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import pyautogui
# Path to your Chrome WebDriver executable
chromedriver_path = '/Users/nickyoung/Downloads/chromedriver-mac-x64/chromedriver'
# Set Chrome options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
# Initialize Chrome WebDriver
driver = webdriver.Chrome(service=Service(executable_path=chromedriver_path), options=chrome_options)
# Navigate to the login page
driver.get('https://luxury3.gameassists.co.uk/gambit/en/registration?atk=wizfulloccupation')
WebDriverWait(driver, 15).until(EC.url_contains('https://luxury3.gameassists.co.uk/gambit/en/registration'))
time.sleep(10) #wait for page to fully load, its slow
#test to access another iframe, works fine
login_iframe_locator = (By.XPATH, '//*[@id="login-proxy"]')
WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it(login_iframe_locator))
#SAME PROCESS FOR THE REGISTRATION IFRAME, BUT THIS FAILS
#reg_iframe_locator = (By.XPATH, '//*[@id="swift-registration-306700405343797-2"]')
#WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it(reg_iframe_locator))
# Instead try to Execute JavaScript to switch to the iframe using its ID
iframe_name = "swift-registration-306700405343797-2"
driver.execute_script(f"document.getElementByName('{iframe_name}').contentWindow.document.body")
# Also Fails
# Wait for the first name input field to be visible
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div/div/div/mgs-text-input-v-01[1]/div/input')))
first_name_input = driver.find_element(By.XPATH, '/html/body/div/div/div/mgs-text-input-v-01[1]/div/input')
# Click on the input field to focus on it
first_name_input.click()
# Send keys to the first name input field
first_name_input.send_keys('ITSAutomation')
# After interacting with elements within the iframe, switch back to the default content
driver.switch_to.default_content()
Thanks
Its because the iframe ID you were using was dynamically generated so your locators fails, You can use below to enter the form data.Also you should try not to use locators with so many indexes.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import pyautogui
# Path to your Chrome WebDriver executable
chromedriver_path = '/Users/nickyoung/Downloads/chromedriver-mac-x64/chromedriver'
# Set Chrome options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
# Initialize Chrome WebDriver
driver = webdriver.Chrome(service=Service(executable_path=chromedriver_path), options=chrome_options)
# Navigate to the login page
driver.get('https://luxury3.gameassists.co.uk/gambit/en/registration?atk=wizfulloccupation')
wait= WebDriverWait(driver, 60)
wait.until(EC.url_contains('https://luxury3.gameassists.co.uk/gambit/en/registration'))
wait.until(EC.presence_of_element_located((By.XPATH, "//iframe[contains(@id,'swift-registration')]")))
iframe = driver.find_element_by_xpath("//iframe[contains(@id,'swift-registration')]")
driver.switch_to.frame(iframe)
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//label[text()='First Name']//following-sibling::input")))
first_name_input = driver.find_element(By.XPATH, "//label[text()='First Name']//following-sibling::input")
# Click on the input field to focus on it
first_name_input.click()
# Send keys to the first name input field
first_name_input.send_keys('ITSAutomation')
# After interacting with elements within the iframe, switch back to the default content
driver.switch_to.default_content()