使用 java 和 selenium 使用输入标签构建时如何填充下拉列表
How to populate drop down when it is built using an input tag using java and selenium
我有一个用 extJS 构建的下拉菜单。
<input id="combo-1786-inputEl" data-ref="inputEl" type="text" size="1" name="Query Category" placeholder="Select query category" role="combobox" aria-hidden="false" aria-disabled="false" aria-readonly="false" aria-invalid="true" aria-required="true" aria-haspopup="true" aria-expanded="false" aria-autocomplete="list" class="x-form-field x-form-required-field x-form-text x-form-text-default x-form-invalid-field x-form-invalid-field-default x-form-empty-field x-form-empty-field-default" autocomplete="off" data-componentid="combo-1786" data-errorqtip="<ul class="x-list-plain"><li>This field is required</li></ul>" aria-describedby="combo-1786-ariaErrorEl">
我们可以看到使用的标签是 'input' 而不是 'select'。
因此,当我查找有关如何填充它的信息时,大多数答案都是在假设它是使用 'select' 标记创建的,但它不起作用。
此外,仅当我单击下拉列表中的箭头时,才会从数据库中获取下拉项:
因此,无法在页面源中找到下拉项目。
有人可以建议如何使用最佳实践填充此类丘陵吗?
P.S-我确实有一个解决方法,但它根本不是好的代码实践,也不是通用的:
driver.findElement(By.xpath("//*[@id='combo-1731-trigger-picker']")).click();//clicking on the arrow key of the drop down.
//Once the drop down item comes, I am trying to replicate pressing the keyboard arrow key,by sending down arrow key to the drop down item(web element)
//This works for me because I know the extact position of my drop down item in the drop down item list.It will stop working if the postion of the drop item changes
//so below loop just presses the down arrow key required number of times.
for(int i=0;i<5;i++){
driver.findElement(By.xpath("//*[@id='combo-1731-inputEl']")).sendKeys(Keys.ARROW_DOWN);
}
driver.findElement(By.xpath("//*[@id='combo-1731-inputEl']")).sendKeys(Keys.ENTER);
如果你阅读了上面代码中提到的注释,你就会明白逻辑是多么脆弱。
请帮忙。
您正在尝试单击/ select 下拉菜单中的项目是否正确?
下拉项是否具有唯一 ID?如果是这样,您应该能够将特定的 xpath id 传递给它。
我个人使用Css来查找元素,那么就是
driver.find_element(By.CSS_SELECTOR,'#combo-1731-trigger-picker').click()
driver.find_element(By.CSS_SELECTOR, '#combo-1731-inputEl > nth:child(x)').click()
其中 x = 下拉项的数量。
或者如果他们有唯一 ID,则使用
driver.find_element(By.CSS_SELECTOR, '#theUniqueIdGoesHere').click()
我写了整整几周的测试,使用 xpath selectors,每天都很痛苦 运行 测试并看着它失败。自从我开始编写自动测试以来,返回并将所有内容更改为 Css select 或让我省去了很多头痛。
编辑:您可以尝试以下操作,
driver.findElement(By.linkText("Your Links Text Here")).click();
这只有在每个链接文本也是唯一的情况下才有效,否则它将select它找到的第一个。
如果这些对你有用,你介意接受我的回答吗?
我有一个用 extJS 构建的下拉菜单。
<input id="combo-1786-inputEl" data-ref="inputEl" type="text" size="1" name="Query Category" placeholder="Select query category" role="combobox" aria-hidden="false" aria-disabled="false" aria-readonly="false" aria-invalid="true" aria-required="true" aria-haspopup="true" aria-expanded="false" aria-autocomplete="list" class="x-form-field x-form-required-field x-form-text x-form-text-default x-form-invalid-field x-form-invalid-field-default x-form-empty-field x-form-empty-field-default" autocomplete="off" data-componentid="combo-1786" data-errorqtip="<ul class="x-list-plain"><li>This field is required</li></ul>" aria-describedby="combo-1786-ariaErrorEl">
我们可以看到使用的标签是 'input' 而不是 'select'。
因此,当我查找有关如何填充它的信息时,大多数答案都是在假设它是使用 'select' 标记创建的,但它不起作用。
此外,仅当我单击下拉列表中的箭头时,才会从数据库中获取下拉项:
因此,无法在页面源中找到下拉项目。
有人可以建议如何使用最佳实践填充此类丘陵吗?
P.S-我确实有一个解决方法,但它根本不是好的代码实践,也不是通用的:
driver.findElement(By.xpath("//*[@id='combo-1731-trigger-picker']")).click();//clicking on the arrow key of the drop down.
//Once the drop down item comes, I am trying to replicate pressing the keyboard arrow key,by sending down arrow key to the drop down item(web element)
//This works for me because I know the extact position of my drop down item in the drop down item list.It will stop working if the postion of the drop item changes
//so below loop just presses the down arrow key required number of times.
for(int i=0;i<5;i++){
driver.findElement(By.xpath("//*[@id='combo-1731-inputEl']")).sendKeys(Keys.ARROW_DOWN);
}
driver.findElement(By.xpath("//*[@id='combo-1731-inputEl']")).sendKeys(Keys.ENTER);
如果你阅读了上面代码中提到的注释,你就会明白逻辑是多么脆弱。 请帮忙。
您正在尝试单击/ select 下拉菜单中的项目是否正确? 下拉项是否具有唯一 ID?如果是这样,您应该能够将特定的 xpath id 传递给它。
我个人使用Css来查找元素,那么就是
driver.find_element(By.CSS_SELECTOR,'#combo-1731-trigger-picker').click()
driver.find_element(By.CSS_SELECTOR, '#combo-1731-inputEl > nth:child(x)').click()
其中 x = 下拉项的数量。
或者如果他们有唯一 ID,则使用
driver.find_element(By.CSS_SELECTOR, '#theUniqueIdGoesHere').click()
我写了整整几周的测试,使用 xpath selectors,每天都很痛苦 运行 测试并看着它失败。自从我开始编写自动测试以来,返回并将所有内容更改为 Css select 或让我省去了很多头痛。
编辑:您可以尝试以下操作,
driver.findElement(By.linkText("Your Links Text Here")).click();
这只有在每个链接文本也是唯一的情况下才有效,否则它将select它找到的第一个。
如果这些对你有用,你介意接受我的回答吗?