excel - 无法使用 Selenium VBA 在 Chrome 中循环 Excel 数据
问题描述
我发现循环功能有困难,即。从 excel 表中获取值并执行任务。我有这个网站:http ://dgftebrc.nic.in:8100/BRCQueryTrade/index.jsp其中我需要输入 IEC 代码:
- 0906008051
- 0906008069
稍后输入
运费单号:
- 3929815
- 3953913
- 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 语句,但是提取的数据被复制了两次。
解决方案
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
推荐阅读
- excel - 尝试使用 VBA 打开和访问其他 Excel 文档中的数据时出现错误消息。OK 允许程序恢复。我该如何避免这个问题?
- python-3.x - 使用 pathlib 获取当前符号目录
- java - 为什么 Java.Time.Year 被任意限制在其原始限制以下?
- html - ShinyDashboard 中的 CSS 反应式值框
- php - Laravel:创建一个假的 belongsToMany withPivot 关系
- r - 从数据框中提取多个平均值的新向量
- javascript - JavaScript 函数 toUpperCase 与 CSS transform-text:uppercase 在性能方面如何比较?
- c - u-boot 我的显示器初始化后可以运行一个函数吗
- python - 如何选择一个好的 Python 列表数据结构来存储所有可能的井字游戏
- reactjs - create-react-app SyntaxError:运行测试时出现意外标识符