我正在尝试访问以下 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()
谢谢
因为你使用的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()