如何通过没有元素定位符的输入字段形式来key.tab和sendkeys?

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

我试图用许多类型的输入字段(文本,单选按钮,数字字段等)填写表单页面,但问题是所有字段都是动态的,因此很难使用。

我想知道是否可以仅通过key.tab遍历每个字段并发送send.keys而无需指定字段的元素?还是我需要以某种方式找到使用父div并将其向下钻取直到找到特定元素的方法。预先感谢!

这是我到目前为止在Selenium C#脚本中拥有的内容:

        public void FillOutForm()
    {
        //this fills out the first text input field: correct
        Driver.FindElement(By.XPath("//input[contains(@id,'sa-txt')]")).SendKeys("Timmy");

        //tab to next input field
        builder.SendKeys(OpenQA.Selenium.Keys.Tab).Build().Perform();

        //this actually enters the text on another field in the form and not the second field: incorrect
        new Actions(Driver).SendKeys("Jones").Perform();

        builder.SendKeys(OpenQA.Selenium.Keys.Tab).Build().Perform();

        Utils.Pause(5000);
    }

也是UI检查ImageofUI的屏幕截图>

我正在尝试用许多类型的输入字段(文本,单选按钮,数字字段等)填写表单页面,但问题是所有字段都是动态的,因此很难使用。我是...

c# selenium automation webdriver browser-automation
1个回答
0
投票

使用绝对xpath并非最佳实践。更好的方法是使用CSS选择器输入[type ='text']并设置一些值来获取文本字段的集合。

© www.soinside.com 2019 - 2024. All rights reserved.