访问iframe问题 - Python Selenium自动化测试

0 投票
1 回答
27 浏览
提问于 2025-04-14 15:28

我正在尝试访问下面的iframe:

但是我尝试的所有方法都无法找到它,因此我无法找到first_name_value这个元素来进行操作和填充文本框。

下面的代码中包含了两种方法,一种是通过xpath正常访问,另一种是硬性定义iframe的名称并使用js。两种方法都不奏效。

这是iframe的HTML,可能没有src属性是个问题?如果是这样,我该如何解决这个问题呢?

<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>

这是代码

 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()

谢谢

1 个回答

1

这是因为你使用的iframe ID是动态生成的,所以你的定位方式失效了。你可以使用下面的代码来输入表单数据。另外,尽量不要使用带有太多索引的定位方式。

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()

撰写回答