在c#中我正在使用以下功能
foreach (HtmlElement item in webBrowser1.Document.GetElementsByTagName("option"))
{
if (item.InnerText == valano.Text)
{
item.SetAttribute("SELECTED", "SELECTED");
}
}
用于选择以下html中的选项
<select id="Suco" name="Suco"><option /><option>14
</option><option>15
</option><option>16
</option><option>17
</option><option>18
</option><option>19
</option><option>20
</option><option>21
</option><option>22
</option><option>23
</option><option>24
</option><option>25
</option></select>
如何使函数在delphi中工作?
var
ovElements: OleVariant;
i: Integer;
begin
ovElements := WebTesta.OleObject.Document.forms.item(0).elements;
for i := 0 to (ovElements.Length - 1) do
if (ovElements.item(i).tagName = 'option') and
(ovElements.item(i).type = 'select') and
(ovElements.item(i).Value = 'Suco') then
ovElements.item(i).Click;
没有用,为什么?
答案 0 :(得分:0)
你的Delphi代码甚至没有接近做C#代码所做的事情。
以下是C#代码的翻译:
var
ovElements, ovItem: OleVariant;
i: Integer;
begin
ovElements := WebTesta.OleObject.Document.GetElementsByTagName('option');
for i := 0 to (ovElements.length - 1) do
begin
ovItem := ovElements.item(i);
if ovItem.InnerText = valano.Text then
item.SetAttribute('SELECTED', 'SELECTED');
end;
end;
或者,试试这个:
var
ovOptions, ovItem: OleVariant;
i: Integer;
begin
ovOptions := WebTesta.OleObject.Document.getElementById('Suco').options;
for i := 0 to (ovOptions.Length - 1) do
begin
ovItem := ovOptions.item(i);
if ovItem.text = valano.Text then
begin
ovItem.selected := True;
Break;
end;
end;
end;
或者这个:
var
ovSelect, ovOptions: OleVariant;
i: Integer;
begin
ovSelect := WebTesta.OleObject.Document.getElementById('Suco');
ovOptions := ovSelect.options;
for i := 0 to (ovOptions.Length - 1) do
begin
if ovOptions.item(i) = valano.Text then
begin
ovSelect.selectedIndex := i;
Break;
end;
end;
end;