按下登录按钮Excel Vba

时间:2018-12-04 15:03:32

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

我有下面的代码,可使用代码登录网站。但是,未单击登录按钮。我是Excel VBA的新手,请问您有什么建议?

Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub Myenter()

Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
MyURL = "https://willemendrees.nl/fn/login/"
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.id_username.Value = "amsterdam@willemendrees.nl" 
HTMLDoc.all.id_password.Value = "*****" 
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
Next
End With
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

2 个答案:

答案 0 :(得分:3)

您可以使用[tabindex='3']的CSS attribute = value选择器。这将通过其tabindex属性值为3来定位登录按钮。

HTMLDoc.querySelector("[tabindex='3']").Click

整件事:

Option Explicit
Public Sub AttemptLogin()
    Dim IE As New InternetExplorer
    With IE
        .Visible = True
        .Navigate2 "https://willemendrees.nl/fn/login/"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            With .querySelector("#id_username")
                .Focus
                .Value = "amsterdam@willemendrees.nl"
            End With
            With .querySelector("#id_password")
                .Focus
                .Value = "***"
            End With
            .querySelector("[tabindex='3']").Click
        End With

        While .Busy Or .readyState < 4: DoEvents: Wend

        Stop '<== Delete me later
        .Quit
    End With
End Sub

答案 1 :(得分:0)

该html页面上似乎有两个表单元素,而登录名是第二个。确定后提交表单。

import React, { Component } from "react";
import { StyleSheet, Text, View } from "react-native";
window.navigator.userAgent = "react-native";
import io from "socket.io-client";

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: "HelloWorld"
    };
    this.socket = io("localhost:8080", { jsonp: false });
  }

  componentDidMount() {
    this.socket.on("update", () => {

      this.setState({ name: "updated name !" });
    });
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.instructions}>{this.state.name}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#F5FCFF"
  },
  welcome: {
    fontSize: 20,
    textAlign: "center",
    margin: 10
  },
  instructions: {
    textAlign: "center",
    color: "#333333",
    marginBottom: 5
  }
});

元素集合的索引号从零开始。