如何用Python填写Iframe内的表单

1 投票
1 回答
1120 浏览
提问于 2025-04-17 12:41

我正在尝试用这个Python脚本自动登录一个网站。

from win32com.client import Dispatch
import win32com.client
import time,ids

IE=Dispatch("InternetExplorer.Application")
IE.Visible=1
IE.Navigate("Source.html")
while IE.ReadyState != 4:    # Wait for browser to finish loading.
    time.sleep(1)
doc = IE.Document # Get the document.
while doc.readyState != "complete": # Wait for document to finish
    time.sleep(1)
doc.Login.USER.value = ids.username    
doc.Login.PASSWORD.value = ids.password
doc.Login.submit() 
while IE.ReadyState != 4:    # Wait for browser to finish loading.
    time.sleep(1)
doc = IE.Document # Get the document.
while doc.readyState != "complete": # Wait for document to finish
    time.sleep(1)
time.sleep(30)
IE.Application.Quit();

在普通网站上运行得很好,但Source.html加载的是一个包含IFrame的网站。也就是说,用户名和密码的输入框是在这个IFrame里面的,我不知道怎么才能把数据输入到那里。

1 个回答

0

也许可以通过它的ID来访问这个元素,也就是iframe。

你加载的页面里有这样的元素:

<iframe id='myframe'>

在你的代码中,可以试试这样写:

doc.myframe.Login.user

你可以打开Python的交互式环境,随便试试你的doc对象,看看怎么找到你想要的元素。

撰写回答