如何在VBA和webelement或selectelement中使用另外两个下拉动态?

问题描述 投票:0回答:1

这些天我和硒和vba一起工作,我怀疑;当我尝试使用下拉“动态”时,我无法理解为什么我的webelement或selecelement不刷新或下拉刷新时出错。

例如,我必须使用一个具有表和两个选择和一个类似输入的按钮的网页,只有在我选择了两个选项时才更改表。

对于这个例子我们会说:

firts select有id Select1。第二个选择有id Select2。表table有id Table_1。按钮输入有id按钮1。

Table1仅在Select1和Select2选择了一个选项时更改或刷新(如果只有一个选择了选项,表格不会更改或刷新)

在开始时打开网页(webdriver)Select1和select2没有选项选择(你知道......“选择一个选项”),当然table_1有什么

为此我使用:

Sub example()

'*// here a put code to initialize web driver and named WEB3

'*// in this part i have webpage opnened so...


'*//here i get my variables

Set GGT = WEB3.FindElementById("Select1").AsSelect
Set CBT = WEB3.FindElementById("Select2").AsSelect
Set SubElement = WEB3.FindElementByClass("button1")

'*//use this for to "play" with webpage

For Each i In GGT.Options
   for each j in CBT.Options
    GGT.SelectByIndex (i.Attribute("index"))
    CBT.SelectByIndex (j.Attribute("index"))
    subelment.click
Next
Next

end sub

好吧,在这一点上表取决于两个选择;

Select1和Select2是独立的,都是静态的,我的意思是,选项总是相同的

子菜单(按钮)刷新表并选择开始(不刷新网页)。

如果我运行代码我没有任何问题Select1取选项(1)然后选择2取其选项(1),(2)等;你知道所有组合和表生成所有表。但如果变换Select2并且现在select2依赖于Select1,我有下一个条件:

Select1是静态的; Select2是dinamic并且依赖于select1,也就是说,假设Select1有3个选项水果,形状和颜色。但是如果Select1是水果,则Select2中的每个选项的Select2更改为select选择2具有选项apple和pears;如果Select1是形状,则select2具有方形和圆形选项,如果select1是颜色,则select2显示红色和蓝色。

这里是我有问题的地方我不明白发生什么事情会产生错误

For Each i In GGT.Options
For Each j In CBT.Options
    GGT.SelectByIndex (i.Attribute("index"))
    CBT.SelectByIndex (j.Attribute("index")) '*//here show error 10 in 
                                                 second iteration
    subelement.click
Next
Next

首先我认为因为Select2更改我必须刷新变量CBT所以添加:设置CBT = WEB3.FindElementById(“Select2”)。在fors之间选择所以......

For Each i In GGT.Options
Set CBT = WEB3.FindElementById("Select2").AsSelect
  For Each j In CBT.Options
    GGT.SelectByIndex (i.Attribute("index"))
    CBT.SelectByIndex (j.Attribute("index")) '*//here show error 10 in 
                                                 second iteration
    subelement.click
  Next
Next

但是仍然没有工作错误,我不知道为什么如果我刷新CBT不要刷新它。我的意思就像是在Excel中,当在单元格(1,1)put = C2 + F3如果我移动单元格F3到r2自动实现功能所以“= C2 + r2”但如果我使用键supr或delet只改变值得到功能(0),但如果我右键单击F3并选择消除单元格生成错误REF,因为单元格F3被消除并放入一个新的F3我认为这是因为这个原因我添加了Set CBT = WEB3.FindElementById(“Select2” )。选择获取新的Select2但不起作用。 CBT sitll得到旧的select2而没有得到新的select2

在我尝试使用显式代码之后,也就是说,不要使用变量CBT,GGT并将所有句子放在一起但仍然相同。

这就是mi问题;如何使用SET或刷新Webelement来选择FOR-FOR中的元素与下拉。

或解释为什么不像其他变量那样刷新变量,我的意思是这项工作

Dim L as integer
L=4
L=5
L=L+5

这项工作

SET H=Webdriver.findelementbyid("kgkj")
SET H=Webdriver.findelementbyid("utut")
SET H=H.asselect

但是,如果我把它放在一个FOR-FOR(我很遗憾FOR FOR FOR for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for

有人可以用FOR-FOR和下拉动态解释为什么或如何做工作。

首先,谢谢

NT:我使用.asselect和findelmentbyid()因为时间以前曾经使用过getelment和Select,但是现在我不知道为什么那个语法还没有用(我认为是因为使用VBA)

excel vba selenium web-scraping drop-down-menu
1个回答
0
投票

您的订购应该有所不同吗?如果2的变化取决于1,则选择1,我希望在设置2和循环2之前发生。

For Each i In GGT.Options
    GGT.SelectByIndex i.Attribute("index") 
    'possible wait condition here
    Set CBT = WEB3.FindElementById("Select2").AsSelect
    For Each j In CBT.Options       
       CBT.SelectByIndex j.Attribute("index") 
       subelement.click
    Next
Next
© www.soinside.com 2019 - 2024. All rights reserved.