首页 > 解决方案 > 无法访问表单域

问题描述

我正在尝试使用我的凭据登录此页面:https ://myibd.investors.com/secure/signin.aspx

当我“检查”它时 - 我可以看到类的形式gigya-login-form 但是当我“查看源代码”时,这种形式不存在。我假设表单是通过 JS(或类似的东西)加载的。我研究了 SO,但没有一个解决方案有帮助。主要是人们建议等待页面加载

等待元素加载 30 秒的代码

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

url = 'https://myibd.investors.com/secure/signin.aspx'

driver = webdriver.Chrome()
driver.get(url)
myElem = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, 'gigya-input-text')))

返回TimeoutException: Message: - 这意味着该元素永远不会加载。

有人可以帮我填写该表格并单击提交吗?谢谢!

UPD。我也尝试进入 iframe 内部,但效果不佳 :( 下面的代码

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("signin-iframe"))

username = driver.find_element_by_name("username")
username.send_keys("my_user_name")

它抛出 ElementNotVisibleException: Message: element not visible (我尝试按照 SO 的想法使其可见,但没有运气)

标签: pythonseleniumselenium-webdriver

解决方案


表单位于 iframe 内,因此为了能够处理输入字段,您需要切换到该 iframe:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("signin-iframe"))
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[@aria-label="Email" and @placeholder="Email *"]')))
username.send_keys("my_user_name")
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[@aria-label="Password" and @placeholder="Password *"]')))
password.send_keys("my_password")
username.submit()

推荐阅读