Excel VBA控制网页与Knockout-填写细节;点击保存;似乎工作;但没有任何保存 [英] Excel VBA controls Webpage with Knockout- Fills out details; clicks on save; appears to work; but nothing is saved

查看:256
本文介绍了Excel VBA控制网页与Knockout-填写细节;点击保存;似乎工作;但没有任何保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:



基本上尝试使用Excel VBA编辑网站。编辑似乎起作用,但是当我使用保存按钮时,不会保存任何内容。我知道保存按钮的作用,下面解释。那么为什么我的更新的数据在屏幕上是可以被保存的?



故事:



我有这个代码,我一直在使用Excel VBA工作了一段时间。它在互联网浏览器中打开一个网页,浏览我想要的内容,填写一些数据,所有数据都显示在屏幕上,使用各种方法,例如:

 对于每个objElement在objElementColl 
ExtractedName = objElement.outerHTML

如果InStr(ExtractedName,NewPermit)> 0然后
objElement.Checked = True

 设置DropDown = objHTML.getElementById(ProjectFile-AccreditedCertifierId)
DropDown.selectedIndex = 1
pre>

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

所有这些都工作得很好,并在屏幕上进行更改。然后使用以下方式点击保存:

 设置objElementColl = objHTML.getElementsByClassName(btn)

对于每个objElement在objElementColl
ExtractedName = objElement.outerHTML

如果InStr(ExtractedName,click:save,enable:)> 0然后
objElement.Click
ExtractedName = 1
退出
结束如果
下一个

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



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



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

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



所以,从上面,它出现我的保存点击工作,但由于某些原因,虽然框被明显改变并使用代码填写,可见和背景之间有差距。



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

 < fieldset> 
< legend>提案< / 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>批量< / label>< input class =form-controldata-bind =textInput:ProjectFile().ProposalLotmaxlength =100name = ProjectFile-ProposalLottype =text/>
< / div>
< div class =col-xs-2 form-groupdata-bind =visible:ProjectFile()StateId()!= 7&&&ProjectFile()StateId()!= 5 >
< label data-bind =text:ProjectFile().ProposalDpLabel()>< / label>
< input class =form-controldata-bind =textInput:ProjectFile().ProposalDpmaxlength =100name =ProjectFile-ProposalDptype =text/>
< / div>

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

  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:新的ViewModel.ObservableField(),名称:新的ViewModel.ObservableField(),ExistingApprovalDate:新的ViewModel.DateField(DD / MM / YYYY),ProjectClosed:新的ViewModel。 ObservableField(),ProposalAddress:new ViewModel.ObservableChildField(exports.AddressViewModel,this),Zoning:new ViewModel.ObservableField(),ProposalLot:new return ProjectFileEditViewModel;})(); if(exports.ProjectFileEditViewModel = = NULL)exports.ProjectFileEditViewModel = ProjectFileEditViewModel; 

还有这个...再长得多:

  Buildaform.model = new Buildaform.ProjectPageViewModel({...,ProposalLot:null ....} 
/ pre>

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



对于坏消息,我不能公开发布网站地址或源代码,但请PM我,我可以工作。

解决方案

根据您的评论:


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


似乎问题是代码设置表单控件,而不是代码点击保存按钮。



这似乎是一个与 VBA 无关的问题, strong> knockout - 请参阅此SO发布。相关评论是:


您的问题是ko订阅了检查的绑定内的点击事件:


该帖子中的提问者对您有类似的问题 - 他们正在尝试复选框(以更改视图),但它没有更新视图模型或底层模型本身。 Knockout是 MVVM 框架。



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



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



根据上述引用的解决方案,再加上这里我将危害下面的代码作为一个可能的解决方案,但请注意它是未经测试的...



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



您的复选框示例:

 如果InStr(ExtractedName,NewPermit)> 0然后
//希望这将在您更改之前取消所需的绑定值
objElement.Click
objElement.Checked = True

您的下拉示例:

 设置DropDown = objHTML .getElementById(ProjectFile-AccreditedCertifierId)
//希望这将在您更改之前取消所需的绑定值
Dropdown.Click
DropDown.selectedIndex = 1

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


Summary:

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?

The story:

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

and

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

or

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

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) Pause my code and manually click save (same issue)

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

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>

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.

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

解决方案

Per your comment that:

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.

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

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

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.

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

Your checkbox example:

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

Your dropdown example:

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

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

这篇关于Excel VBA控制网页与Knockout-填写细节;点击保存;似乎工作;但没有任何保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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