VBA Excel, Selenium - 遍历单选按钮值以查找所选值并打印它
VBA Excel, Selenium - Loop through radio button values to find the selected value and print it
我想知道选择了哪个单选按钮选项。我正在寻找一种循环单选按钮响应并打印带有 checked = 'Checked' 的方法。我尝试过的一切 returns 所有选项或 returns 什么都没有。
这是 html 的片段
<tr onmouseover="this.style.backgroundColor='#f46f66';" onmouseout="this.style.backgroundColor='#d3e2e5';" style="background-color: rgb(155, 300, 59);">
<td width="80%">Select a fruit</td><td width="20%">
<input type="radio" name="FruitOptions" value="Peach" checked="checked" disabled="disabled">Peach<br>
<input type="radio" name="FruitOptions" value="Apple" disabled="disabled">Apple<br></td></tr>
<input type="radio" name="FruitOptions" value="Orange" disabled="disabled">Orange<br></td></tr>
我正在使用 vba selenium 来抓取网站以获取此信息。
这是我试过的方法
enter code here
Chrome.Wait 100
Set seconds = Chrome.FindElementsByTag("tr")
j = 2
s = 2
Set mysheet = Sheets("Sheet7")
For Each second In seconds
On Error Resume Next
mysheet.Cells(j, s).Value = second.FindElementsByTag("td")(2).Text
Set inputvs = second.FindElementsByTag("input")
For Each inputv In inputvs
If inputv.Attribute("checked").Value = "checked" Then
'This is where my issue is
'I want to print the value with the checked attribute
Debug.Print (inputv.Attribute("value"))
mysheet.Cells(j, s).Value = inputv.getAttribute("value")
Else
Debug.Print ("none")
End If
Next
j = j + 1
Next
使用 attribute = value css 选择器,无需循环。您为值 = checked
的 checked
属性指定 css 模式,该模式出现在一个元素中,该元素还具有值 FruitOptions
的 name
属性。
Debug.Print chrome.FindElementByCss("[name=FruitOptions][checked=checked]").Attribute("value")
我还想补充一下我的想法,以帮助其他遇到类似情况的人。如果我循环使用一组单选按钮,QHarr 提供的答案会很好,但我从中获取此信息的表单有几个。
这就是我最后做的。
Chrome.Wait 100
Set seconds = Chrome.FindElementsByTag("tr")
j = 2
s = 2
Set mysheet = Sheets("Sheet7")
For Each second In seconds
On Error Resume Next
mysheet.Cells(j, s).Value = second.FindElementsByTag("td")(2).Text
Set inputvs = second.FindElementsByTag("input")
For Each inputv In inputvs
If inputv.Attribute("checked") = True Then
mysheet.Cells(j, s).Value = (inputv.Attribute("value"))
Else
End If
Next
j = j + 1
Next
我想知道选择了哪个单选按钮选项。我正在寻找一种循环单选按钮响应并打印带有 checked = 'Checked' 的方法。我尝试过的一切 returns 所有选项或 returns 什么都没有。
这是 html 的片段
<tr onmouseover="this.style.backgroundColor='#f46f66';" onmouseout="this.style.backgroundColor='#d3e2e5';" style="background-color: rgb(155, 300, 59);">
<td width="80%">Select a fruit</td><td width="20%">
<input type="radio" name="FruitOptions" value="Peach" checked="checked" disabled="disabled">Peach<br>
<input type="radio" name="FruitOptions" value="Apple" disabled="disabled">Apple<br></td></tr>
<input type="radio" name="FruitOptions" value="Orange" disabled="disabled">Orange<br></td></tr>
我正在使用 vba selenium 来抓取网站以获取此信息。
这是我试过的方法
enter code here
Chrome.Wait 100
Set seconds = Chrome.FindElementsByTag("tr")
j = 2
s = 2
Set mysheet = Sheets("Sheet7")
For Each second In seconds
On Error Resume Next
mysheet.Cells(j, s).Value = second.FindElementsByTag("td")(2).Text
Set inputvs = second.FindElementsByTag("input")
For Each inputv In inputvs
If inputv.Attribute("checked").Value = "checked" Then
'This is where my issue is
'I want to print the value with the checked attribute
Debug.Print (inputv.Attribute("value"))
mysheet.Cells(j, s).Value = inputv.getAttribute("value")
Else
Debug.Print ("none")
End If
Next
j = j + 1
Next
使用 attribute = value css 选择器,无需循环。您为值 = checked
的 checked
属性指定 css 模式,该模式出现在一个元素中,该元素还具有值 FruitOptions
的 name
属性。
Debug.Print chrome.FindElementByCss("[name=FruitOptions][checked=checked]").Attribute("value")
我还想补充一下我的想法,以帮助其他遇到类似情况的人。如果我循环使用一组单选按钮,QHarr 提供的答案会很好,但我从中获取此信息的表单有几个。
这就是我最后做的。
Chrome.Wait 100
Set seconds = Chrome.FindElementsByTag("tr")
j = 2
s = 2
Set mysheet = Sheets("Sheet7")
For Each second In seconds
On Error Resume Next
mysheet.Cells(j, s).Value = second.FindElementsByTag("td")(2).Text
Set inputvs = second.FindElementsByTag("input")
For Each inputv In inputvs
If inputv.Attribute("checked") = True Then
mysheet.Cells(j, s).Value = (inputv.Attribute("value"))
Else
End If
Next
j = j + 1
Next