首页 > 解决方案 > vba中的SeleniumError

问题描述

我正在尝试进行 Selenium 测试并将其转换为 excel VBA 代码。

当我尝试编写代码时,我遇到了 Selenium 错误 [我在这里几乎没用:( ]

我可以获得有关 vba 错误的指导吗?

其次,关于如何执行 vba 代码的任何建议

错误是附加的VBA 错误

我目前拥有的vba代码是

Sub Buy()

    Dim driver As New Selenium.ChromeDriver
    Dim count As Long
    
    Sheets("Input").Activate

    Set driver = CreateObject("Selenium.ChromeDriver")
    count = 1
    
    ' Code assumes all VAT numbers in Column A are valid
    ' No error checking is included here in case they are not
    '
    While (Len(Range("A" & count)) > 0)
    
        driver.Get "https://www.mycode.com.au/Portfolio/Details/12635600"
        
    driver.FindElementById("New Buy").Click
    driver.FindElementById("New Buy").Click
    driver.FindElement(By.ID("SecurityCode")).SendKeys ("JBH")
    driver.FindElement(By.ID("NumberOfShares")).SendKeys ("500")
    driver.FindElement(By.ID("SharePrice")).SendKeys ("20")
    driver.FindElement(By.ID("Fees")).SendKeys ("9.5")
    driver.FindElement(By.ID("Submit")).Click
        
        ' Uncomment the next line if you need to introduce a delay in the browser
        ' to allow it to load the results page
        'Driver.Wait 1000
        
        Range("B" & count) = driver.FindElementByXPath("//table/tbody/tr[6]/td[2]").Text
        
        count = count + 1
    
    Wend
    
    driver.Quit
        
End Sub

我试图模拟的 Selenium IDE 文件是

{
  "id": "d4a3caeb-1fd0-4a30-8c17-d69d7a1b6e36",
  "version": "2.0",
  "name": "Test 10",
  "url": "https://www.mycode.com.au",
  "tests": [{
    "id": "880b98ad-ce57-416d-abab-d5b9eac336cf",
    "name": "Test 10",
    "commands": [{
      "id": "9c84d13e-49f7-4e1e-8e6b-45a1b9654a87",
      "comment": "",
      "command": "open",
      "target": "/Portfolio/Details/126356",
      "targets": [],
      "value": ""
    }, {
      "id": "57eb50bf-f996-4e20-b9c2-15f476e43324",
      "comment": "",
      "command": "setWindowSize",
      "target": "1552x840",
      "targets": [],
      "value": ""
    }, {
      "id": "b627d6f1-2d7a-4b92-bdf6-80c400f787e5",
      "comment": "",
      "command": "click",
      "target": "linkText=New Buy",
      "targets": [
        ["linkText=New Buy", "linkText"],
        ["css=.btn-order-buy", "css:finder"],
        ["xpath=(//a[contains(text(),'New Buy')])[2]", "xpath:link"],
        ["xpath=//a[@onclick='return false;']", "xpath:attributes"],
        ["xpath=//div[@id='active-tab']/div[3]/div[2]/div/div/div/div/a", "xpath:idRelative"],
        ["xpath=(//a[contains(@href, '/PortfolioTransaction/NewBuy?portfolioId=12635600')])[3]", "xpath:href"],
        ["xpath=//div[2]/div/div/div/div/a", "xpath:position"]
      ],
      "value": ""
    }, {
      "id": "f4d72730-3eaa-44e7-85c3-1c63a79032bc",
      "comment": "",
      "command": "type",
      "target": "css=div:nth-child(3) > #SecurityCode",
      "targets": [
        ["css=div:nth-child(3) > #SecurityCode", "css:finder"],
        ["xpath=(//input[@id='SecurityCode'])[4]", "xpath:attributes"],
        ["xpath=//div[@id='page-trade-details']/div/fieldset/div/div[3]/input", "xpath:idRelative"],
        ["xpath=//fieldset/div/div[3]/input", "xpath:position"]
      ],
      "value": "JBH"
    }, {
      "id": "2a6976b9-7364-4dab-bbaf-ebdc214cbe2c",
      "comment": "",
      "command": "type",
      "target": "id=NumberOfShares",
      "targets": [
        ["id=NumberOfShares", "id"],
        ["name=NumberOfShares", "name"],
        ["css=#NumberOfShares", "css:finder"],
        ["xpath=//input[@id='NumberOfShares']", "xpath:attributes"],
        ["xpath=//div[@id='page-trade-details']/div/fieldset/div/div[4]/div[2]/input", "xpath:idRelative"],
        ["xpath=//div[4]/div[2]/input", "xpath:position"]
      ],
      "value": "500"
    }, {
      "id": "1bdbb4a4-221c-4e5f-87d7-3a6e0c9e3ad4",
      "comment": "",
      "command": "type",
      "target": "id=SharePrice",
      "targets": [
        ["id=SharePrice", "id"],
        ["name=SharePrice", "name"],
        ["css=#SharePrice", "css:finder"],
        ["xpath=//input[@id='SharePrice']", "xpath:attributes"],
        ["xpath=//div[@id='page-trade-details']/div/fieldset/div/div[4]/div[3]/input", "xpath:idRelative"],
        ["xpath=//div[4]/div[3]/input", "xpath:position"]
      ],
      "value": "20"
    }, {
      "id": "e8a85e6f-30bc-4a9f-a13e-bc591ac24d0b",
      "comment": "",
      "command": "type",
      "target": "id=Fees",
      "targets": [
        ["id=Fees", "id"],
        ["name=Fees", "name"],
        ["css=#Fees", "css:finder"],
        ["xpath=//input[@id='Fees']", "xpath:attributes"],
        ["xpath=//div[@id='page-trade-details']/div/fieldset/div/div[4]/div[4]/input", "xpath:idRelative"],
        ["xpath=//div[4]/div[4]/input", "xpath:position"]
      ],
      "value": "9.5"
    }, {
      "id": "3bbf2177-6e3e-4fa1-a00b-f5eff9ffe553",
      "comment": "",
      "command": "click",
      "target": "css=#page-trade-details #Submit",
      "targets": [
        ["css=#page-trade-details #Submit", "css:finder"],
        ["xpath=(//input[@id='Submit'])[3]", "xpath:attributes"],
        ["xpath=//div[@id='page-trade-details']/div[2]/input[2]", "xpath:idRelative"],
        ["xpath=//div[2]/form/div/div[2]/input[2]", "xpath:position"]
      ],
      "value": ""
    }]
  }],
  "suites": [{
    "id": "03623809-e8f4-4f37-a9dc-548b1aba1757",
    "name": "Default Suite",
    "persistSession": false,
    "parallel": false,
    "timeout": 300,
    "tests": ["880b98ad-ce57-416d-abab-d5b9eac336cf"]
  }],
  "urls": ["https://www.mycode.com.au/"],
  "plugins": []
}

标签: vbaselenium

解决方案


感谢您的回复。

首先,“混合语言” - 你能更具体一点吗

其次,“您将 selenium 设置为自动实例化,然后使用后期绑定设置驱动程序引用。” - 你能再具体点吗

我很抱歉,但我的技术水平很差。对于我自己来说,这是一个独立的项目,旨在实现将数据输入网络表单的效率结果。

可以忽略对增值税的引用。该代码是从网站中提取的,以便我开始使用 vba。增值税与我的任务无关。

我已将输出发送到 python,如下所示

# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestTest10():
  def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {}
  
  def teardown_method(self, method):
    self.driver.quit()
  
  def test_test10(self):
    self.driver.get("https://www.mywebsite.com.au/Portfolio/Details/12635600")
    self.driver.set_window_size(1552, 840)
    self.driver.find_element(By.LINK_TEXT, "New Buy").click()
    self.driver.find_element(By.CSS_SELECTOR, "div:nth-child(3) > #SecurityCode").send_keys("JBH")
    self.driver.find_element(By.ID, "NumberOfShares").send_keys("500")
    self.driver.find_element(By.ID, "SharePrice").send_keys("20")
    self.driver.find_element(By.ID, "Fees").send_keys("9.5")
    self.driver.find_element(By.CSS_SELECTOR, "#page-trade-details #Submit").click()

有了这个,我修改了我的 vba 代码如下:

Sub Buy()

    Dim driver As New Selenium.ChromeDriver
    Dim count As Long
    
    Sheets("Input").Activate

    Set driver = CreateObject("Selenium.ChromeDriver")
    count = 1
    

    While (Len(Range("A" & count)) > 0)
    
    driver.Get "https://www.mywebsite.com.au/Portfolio/Details/12635600"
        
    driver.FindElement(By.LINK_TEXT, ("New Buy")).Click
    driver.FindElement(By.CSS_SELECTOR, "div:nth-child(3) > #SecurityCode").SendKeys ("JBH")
    driver.FindElement(By.ID("NumberOfShares")).SendKeys ("500")
    driver.FindElement(By.ID("SharePrice")).SendKeys ("20")
    driver.FindElement(By.ID("Fees")).SendKeys ("9.5")
    driver.FindElement(By.ID("Submit")).Click
        
               
        Range("B" & count) = driver.FindElementByXPath("//table/tbody/tr[6]/td[2]").Text
        
        count = count + 1
    
    Wend
    
    driver.Quit
        
End Sub

在运行 vba 时,我收到“需要运行时错误 '424' 对象”。我无法弄清楚如何克服这个错误。


推荐阅读