Excel VBA使用Knockout控制网页;似乎工作;但什么都没有得救 [英] Excel VBA controls Webpage with Knockout; appears to work; but nothing is saved

查看:127
本文介绍了Excel VBA使用Knockout控制网页;似乎工作;但什么都没有得救的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:

基本上尝试使用Excel VBA编辑网站。编辑似乎有效,但是当我使用保存按钮时,不保存任何内容。我知道保存按钮有效,如下所述。那么为什么我的更新数据不会在屏幕上显示?

Basically trying to edit a website with Excel VBA. The edits appear to work, but when I use the save button, nothing is saved. I know the save button works, explained below. So why isn't my updated data, which is visible on the screen being saved?

故事:

我有这个代码,我已经使用Excel VBA工作了一段时间。它在Internet Explorer中打开一个网页,在我想要的地方导航,填写一大堆数据,所有数据都显示在屏幕上,使用各种方法,例如:

I have this code I have been working on for awhile with Excel VBA. It opens a webpage in internet explorer, navigates where I want, fills out a bunch of data, all which show up on the screen, using various methods, such as:

For Each objElement In objElementColl
ExtractedName = objElement.outerHTML

If InStr(ExtractedName, "NewPermit") > 0 Then
objElement.Checked = True

Set DropDown = objHTML.getElementById("ProjectFile-AccreditedCertifierId")
DropDown.selectedIndex = 1

objHTML.getElementsByName(ElementName)(0).Value = ValueCheck

所有这些工作都很顺利,并在屏幕上进行了更改。然后我使用以下方式单击保存:

All of which work very well, and changes on the screen. I then click save by using:

Set objElementColl = objHTML.getElementsByClassName("btn")

For Each objElement In objElementColl
    ExtractedName = objElement.outerHTML

    If InStr(ExtractedName, "click: save, enable:") > 0 Then
        objElement.Click
        ExtractedName = 1
        Exit For
    End If
Next

再次正常工作。问题是它实际上并没有从上面的3个代码中保存我的更改。我试过的是

Which again works fine. The issue is that it doesn't actually save my changes from the code from the 3pieces above. What i have tried is

a)暂停我的代码并手动点击保存(同一问题)

a) Pause my code and manually click save (same issue)

b)暂停我的代码,手动更改复选框并运行代码进行保存(保存手动更改,但不保存编码的

b) Pause my code, manually change a checkbox and run the code to save (does save the manual change, but not the coded ones

c)暂停代码并手动更改框和手动保存(仅保存手动更改的框)

c) Pause the code and manually change a box and manually save (only manually changed box is saved)

因此,从上面看,我的保存点击工作,但出于某种原因,虽然框明显改变并且使用代码填写,可见和背景之间存在差距。

So, from above, it appear my save click works, but for some reason, although the boxes are visibly changed and filled out using the code, there is a gap between the visible and the background.

无论如何,一些HTML源代码。在检查我正在更改的元素时,Chrome会显示什么:

Anyway, some HTML source code. Is what chrome shows me when Inspecting an element I am changing:

    <fieldset>
        <legend>Proposal</legend>

        <div class="col-xs-12 col-sm-8 col-md-6">
            <div class="row">
                <div class="col-xs-2 form-group">
                    <label for="ProjectFile_ProposalLot">Lot</label><input class="form-control" data-bind="textInput: ProjectFile().ProposalLot" maxlength="100" name="ProjectFile-ProposalLot" type="text" />
                </div>
                    <div class="col-xs-2 form-group" data-bind="visible: ProjectFile().StateId() != 7 && ProjectFile().StateId() != 5">
                        <label data-bind="text: ProjectFile().ProposalDpLabel()"></label>
                        <input class="form-control" data-bind="textInput: ProjectFile().ProposalDp" maxlength="100" name="ProjectFile-ProposalDp" type="text" />
                        </div>

我还搜索了页面的完整源代码......我相信这可能很重要,但我不是HTML编码器。我已经缩短了一点

I have also searched the entire source code for the page... I believe this might be important, but I am not a HTML coder. I have shortened it a bit

var ProjectFileEditViewModel=(function(){__extends(ProjectFileEditViewModel,ViewModel.Model);function ProjectFileEditViewModel(){ProjectFileEditViewModel.__super__.constructor.apply(this,arguments);};ProjectFileEditViewModel.prototype.fields=function(){return {"Id":new ViewModel.NumberField(0),"StateId":new ViewModel.NumberField(0),"DefaultOfficeAddressId":new ViewModel.ObservableField(),"Name":new ViewModel.ObservableField(),"ExistingApprovalDate":new ViewModel.DateField("DD/MM/YYYY"),"ProjectClosed":new ViewModel.ObservableField(),"ProposalAddress":new ViewModel.ObservableChildField(exports.AddressViewModel,this),"Zoning":new ViewModel.ObservableField(),"ProposalLot":new return ProjectFileEditViewModel;})();if(exports.ProjectFileEditViewModel==null)exports.ProjectFileEditViewModel=ProjectFileEditViewModel;

还有这个...再长一点:

There is also this... again much longer:

Buildaform.model=new Buildaform.ProjectPageViewModel({ ... ,"ProposalLot":null .... }

我认为这最后一个与它有关,我不知道我是否可以改变它。

I think this last one has something to do with it, and I do not know if I can change it.

对于坏消息,我不能公开发布网站地址或源代码。但请PM我,我可以解决一些问题。

For the bad news, I cannot release the website address or source code publicly. But please PM me, and I can work something out.

推荐答案

根据您的评论:


b)暂停我的代码,手动更改复选框并运行代码保存
(保存手动更改,但不保存编码的

b) Pause my code, manually change a checkbox and run the code to save (does save the manual change, but not the coded ones

似乎问题出在代码设置上表单控件而不是单击保存按钮的代码。

It seems that the problem is with the code setting form controls and not with the code clicking the save button.

这似乎是与 VBA 无关的问题,但与淘汰 - 参见此SO帖子。相关评论是:

This seems to be a problem not related to VBA but with the behaviour of knockout - see this SO post. The pertinent comment is:


您的问题是ko订阅了检查绑定中的click事件:

Your problem is that ko subscribes on the click event inside the checked binding:

该帖子中的提问者遇到了类似的问题 - 他们正试图检查一个复选框(以更改视图)但它不更新viewmodel或底层模型本身。 Knockout是 MVVM 框架。

The questioner in that post is having a similar problem to you - they are trying to check a checkbox (to change the view) but it is not updating either the viewmodel, or the underlying model itself. Knockout is a MVVM framework.

您的问题中的赠品是您的手动更改提交,因为您在浏览器中通过点击操作执行操作时执行单击和更改,但是你的程序化方法只对表单控件进行了更改,而不是先点击。

The give-away in your question is that your manual changes commit because you perform a click-and-change when performing the action via point-and-click in the browser, but your programmatic method only does the change to the form control, but not the click first.

那么,如何通过IE通过VBA自动化来解决这个问题呢?

So, how to solve this via VBA automation through IE?

根据上面引用的帖子中的解决方案,加上方法这里我会将下面的代码作为一种可能的解决方案,但请注意它是未经测试的......

Based on the solution in the post I referenced above, plus the method here I will hazard the code below as a possible solution, but please note it is untested ...

基本上,您需要单击要更改的表单元素 - 然后更新控件值。希望点击位意味着淘汰视图模型会根据更改进行更新,并且从那里,模型数据将被写入数据库(或其他):

Basically you need to 'click' on the form element you want to change - and then update the control value. Hopefully the 'clicking' bit will mean that the knockout viewmodel updates per the 'change', and from there, the model data will be written to the database (or whatever):

你的复选框示例:

If InStr(ExtractedName, "NewPermit") > 0 Then
// hopefully this will get knockout to apply the required binding before your change the value
objElement.Click
objElement.Checked = True

您的下拉示例:

Set DropDown = objHTML.getElementById("ProjectFile-AccreditedCertifierId")
// hopefully this will get knockout to apply the required binding before your change the value
Dropdown.Click
DropDown.selectedIndex = 1

希望有所帮助 - 相当3管问题!祝你好运。

Hope that helps - quite the 3-pipe problem! Good luck.

这篇关于Excel VBA使用Knockout控制网页;似乎工作;但什么都没有得救的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆