首页 > 解决方案 > 无法使用 Selenium VBA 在 Chrome 中循环 Excel 数据

问题描述

我发现循环功能有困难,即。从 excel 表中获取值并执行任务。我有这个网站:http ://dgftebrc.nic.in:8100/BRCQueryTrade/index.jsp其中我需要输入 IEC 代码:

  1. 0906008051
  2. 0906008069

稍后输入

运费单号:

  1. 3929815
  2. 3953913
  3. 3979509

我的意思是,现在我已经硬编码,但我没有让它在循环中运行,因为我有大量数据并且我不能以硬编码方式编写它,因为对于 1 个 IEC 编号,我有大约 1000 SB.no,所以可以有 100 个 IEC 编号,因此有很多运输单 (SB.no) 编号。

使用我已经解决的标签,然后使用多个运输单号给我带来了问题。

我可以管理这么多代码:

Option Explicit

Public Sub multipletabtest()

Dim bot As WebDriver
Dim keys As New Selenium.keys
Dim count As Long

Set bot = New WebDriver
bot.Start "Chrome"
'count = 1
'While (Len(Range("A" & count)) > 0)

bot.Get "http://dgftebrc.nic.in:8100/BRCQueryTrade/index.jsp"

bot.FindElementByXPath("//input[@type='text'][@name='iec']").SendKeys "0906008051"
bot.FindElementByXPath("//input[@type='text'][@name='sno']").SendKeys "3929815"
bot.Wait 10000         'Time to enter the captcha

bot.FindElementByCss("[value='Show Details']").SendKeys keys.Control, keys.Enter          'Take the value from final result sheet
bot.SwitchToNextWindow

ThisWorkbook.Sheets("Sheet1").Range("B1") = bot.FindElementByXPath("//text()[.='Used']/ancestor::td[1]").Text
'Range("B" & count) = bot.FindElementByXPath("//text()[.='Used']/ancestor::td[1]").Text  'To extract the data
'bot.Window.Close


bot.SwitchToPreviousWindow
bot.FindElementByXPath("//input[@type='text'][@name='sno']").Clear

bot.FindElementByXPath("//input[@type='text'][@name='sno']").SendKeys "3953913"
bot.FindElementByCss("[value='Show Details']").SendKeys keys.Control, keys.Enter
bot.SwitchToNextWindow

ThisWorkbook.Sheets("Sheet1").Range("B2") = bot.FindElementByXPath("//text()[.='Used']/ancestor::td[1]").Text
'Range("B" & count) = bot.FindElementByXPath("//text()[.='Used']/ancestor::td[1]").Text


'count = count + 1
'Wend
bot.Quit
End Sub

如果有人想知道为什么Ctrl+Enter,那是因为验证对于其他运费单号保持不变,所以选择了这种方法。

我也尝试过 while 语句,但是提取的数据被复制了两次。

看图片

标签: excelvbaseleniumselenium-webdriver

解决方案


Option Explicit
Public Sub multipletabtest()

Dim bot As WebDriver
Dim keys As New Selenium.keys
Dim count As Long

Set bot = New WebDriver
bot.Start "Chrome"
bot.Get "http://dgftebrc.nic.in:8100/BRCQueryTrade/index.jsp"

count = 1
While (Len(Range("A" & count)) > 0)


bot.FindElementByXPath("//input[@name='iec']").SendKeys Range("A" & count)
bot.FindElementByXPath("//input[@name='sno']").SendKeys Range("B" & count)

bot.Wait 10000         'Time to enter the Captcha


bot.FindElementByCss("[value='Show Details']").SendKeys keys.Control, keys.Enter
bot.SwitchToNextWindow


If bot.FindElementsByXPath("//tr[2]//td[7]").count > 0 Then
Range("C" & count) = bot.FindElementByXPath("//tr[2]//td[7]").Text

If bot.FindElementsByXPath("//p[contains(text(),'No Data Found.....check the Input Parameters')]").count > 0 Then
Range("C" & count) = "No Data"


End If
End If
bot.Window.Close

bot.SwitchToPreviousWindow
bot.FindElementByXPath("//input[@type='text'][@name='sno']").Clear


count = count + 1
Wend

bot.Quit
End Sub

推荐阅读