Excel VBA - 更改输入字段的值

时间:2018-05-12 03:45:55

标签: html excel vba excel-vba web-scraping

我对excel vba相对较新,并尝试实现以下目标:

我知道网上有很多类似的问题,但我试图找到解决方案而失败了。我想用vba登录网站。因此,我需要输入电子邮件地址和密码。但是,不知何故,如果我更改字段的值,网站仍在等待文本输入?我做错了吗?

这是登录字段的Html:

<div class="login">
    <div class="top">
        <a class="sprd-link" ng-href=""><svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 32 32" key="sprd-heart">
    <!----><!----><!---->
    <!----><!---->
<path d="M 21.1 3.8 L 16 9 l -5.1 -5.1 l -9.6 9.6 L 16 28.2 l 14.8 -14.7 l -9.7 -9.7 Z M 16 23.7 L 5.7 13.4 l 5.1 -5.1 l 5.2 5.2 l 5 -5.1 l 5.1 5.1 L 16 23.7 Z" /></svg></a>
    </div>
    <div class="login-container">
        <div class="left">
            <div>
                <h1 class="text-center">Log in to your account</h1>
            </div>
        </div>
        <div class="right">
            <img class="rocket" src="/images/rocket_launched.png">
            <login-form><div class="login-form" ng-class="{'login-error': vm.loginError}">
    <form name="loginForm" class="ng-pristine ng-invalid ng-invalid-required" novalidate="" ng-submit="vm.login()" data-dpmaxz-fid="1">
        <p class="login-field has-error" ng-class="{ 'has-error' : (loginForm.username.$touched || loginForm.$submitted) &amp;&amp; loginForm.username.$error.required }">
            <sprd-label-input><div class="label-input-wrapper" ng-class="{'active': focused || !!inputValue.length || !!modelValue.length || placeholderExists, 'filled': inputValue.length || modelValue.length}" ng-transclude="">
                <label for="loginUsername">Email or username</label>
                <input name="username" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" id="loginUsername" autofocus="" required="" type="text" ng-change="vm.loginDataChange()" sprd-form-autofill="" ng-model="vm.credentials.username" data-dpmaxz-eid="1">
                <small class="error-info error-info-name">
                    Please enter your e-mail address and user name.
                </small>
            </div></sprd-label-input>
        </p>
        <p class="login-field password" ng-class="{ 'has-error' : loginForm.password.$error.required &amp;&amp; (loginForm.password.$touched  || loginForm.$submitted) }">
            <sprd-label-input><div class="label-input-wrapper" ng-class="{'active': focused || !!inputValue.length || !!modelValue.length || placeholderExists, 'filled': inputValue.length || modelValue.length}" ng-transclude="">
                <label for="loginPassword">Enter password</label>
                <input name="password" class="form-control ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" id="loginPassword" required="" type="password" ng-change="vm.loginDataChange()" sprd-form-autofill="" ng-model="vm.credentials.password" data-dpmaxz-eid="2">
                <small class="error-info error-info-name">
                    Please enter your password.
                </small>
            </div></sprd-label-input>
        </p>
        <div class="login-field sprd-checkbox-item" ng-click="vm.credentials.rememberMe = !vm.credentials.rememberMe">
            <sprd-checkbox class="pull-left" checked="vm.credentials.rememberMe"><div class="sprd-checkbox-container">
    <!---->

    <!----><button class="sprd-checkbox" type="button" ng-class="{'active': checked}" ng-if="model == undefined" ng-disabled="disabled">
        <div><svg xmlns="http://www.w3.org/2000/svg" class="icon" viewBox="0 0 16 16" key="accept">
    <!----><!----><!---->
    <!----><!---->
<path d="M 13.3 3.1 l -7.1 7 l -3.5 -3.5 L 1.3 8 l 4.9 4.9 l 8.5 -8.4 Z" /></svg></div>
    </button><!---->
</div>
</sprd-checkbox>
            <div class="sprd-checkbox-label">Stay logged in on this device.</div>
        </div>
        <p class="login-submit">
            <button disabled="disabled" class="btn btn-primary btn-block" id="login-button" type="submit" ng-disabled="vm.loginDisabled || loginForm.$invalid">Login</button>
        </p>
        <a class="link" id="forgot-link" href="https://www.spreadshirt.com/password?redirectBack=true" ng-href="https://www.spreadshirt.com/password?redirectBack=true">Forgot your username or password?</a>
    </form>




    <!---->
</div>
</login-form>
            <div class="register-link-container">
                <span>Don't have an account?</span>
                <a class="link register-link" href="#" ng-click="NavbarCtrl.backToRegisterOrLandingPage()">Register now</a>
            </div>
        </div>
    </div>
</div>

我已经尝试过使用以下方法,但由于网站似乎不接受输入,因此无法正常工作。

objIE.document.getElementByID("loginUsername").value = userName

我使用sendKeys选项获得了成功,但我宁愿避免使用它。

objIE.document.getElementByID("loginUsername").click
objIE.document.getElementByID("loginUsername").focus
application.sendKeys(userName)

原始网站网址如下:

https://partner.spreadshirt.com/login

非常感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:4)

以下对我有用。

DefaultBatchConfigurer