【发布时间】:2018-04-20 06:46:57
【问题描述】:
我有两个输入字段来输入具有相同类别、类型的选项。 id 不同,它是动态的,并且在运行时创建,所以我不能使用 id。我使用了索引,它不能正常工作。
driver.findElement(By.xpath("//input[@type='text'][@placeholder='Provide a response entry that customers can select'][1]")).click();
driver.findElement(By.xpath("//input[@type='text'][@placeholder='Provide a response entry that customers can select'][1]")).sendKeys("Iphone 6");
driver.findElement(By.xpath("//input[@type='text'][@placeholder='Provide a response entry that customers can select'][2]")).click();
driver.findElement(By.xpath("//input[@type='text'][@placeholder='Provide a response entry that customers can select'][2]")).sendKeys("Iphone 7");
我在给定的图片链接中使用了索引。
click link to view code in organized way
索引 1 在这种情况下有效,但无法找到索引 2。
鉴于检查的 html 代码位于输入字段 1 和字段 2 的下方
【问题讨论】:
-
请阅读为什么是screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的相关 HTML、代码试验和错误堆栈跟踪来更新问题。
-
用它的 id 试试。这不一样。一个是“input_168”,另一个是“input_170”。
-
是的,我知道,但 id 是动态的,并且在运行时会发生变化,例如我第一次运行测试可能是 id 值可以是“input_168”和“input_170”。当我第二次运行测试时,它可能是“input_262”和“input_264”
-
@DebanjanB 好的,我提到了代码。首先,我认为它看起来不太可读。
-
@MuneebAkhtar 我已经缩进了您的代码试验,但为了获得有效的答案,您仍然需要提供文本格式的相关 HTML
标签: selenium testing selenium-webdriver automation automated-tests