我有一个用于药物列表的动态搜索框。药物列表将根据用户输入的前 3 个字母显示: 我有盒子的 xpath://input[@placeholder='Search for a medication...']" 但我找不到药物 list ,因为它是动态的。我尝试了很多不同的东西,但没有一个在工作。请参阅下面的列表。任何建议表示赞赏,谢谢!
第一次尝试
public void medSearch(String value) throws Throwable{
By medSearch = By.xpath("//input[@placeholder='Search for a medication...']");
// String medSearch = "TETTERINE, Topical Ointment";
if(waitForPresenceOfElement(medSearch, 20)){
type(medSearch,value);
moveToElementAndClick(medSearch);
}
}
2nd try
public void select_med()throws Throwable {
WebElement medSearch2 = Driver.findElement(By.xpath("//*[normalize-space(text()) and normalize-space(.)='View More Info'])[1]/following::input[1] "));
//if(waitForPresenceOfElement(medSearch2, 20)){
Select dropdown = new Select(medSearch2);
dropdown.selectByVisibleText("TETTERINE, Topical Ointment");
// dropdown.selectByValue("");
}
3rd try
public void med4()throws Throwable {
WebElement listbox = Driver.findElement(By.xpath("//input[@placeholder='Search for a medication...']"));
listbox.sendKeys("tett");
listbox.sendKeys(Keys.PAUSE);
listbox.sendKeys(Keys.DOWN);
listbox.sendKeys(Keys.ENTER);
}
4th try:
/* public void medSearch3(String value) throws Throwable{
WebElement listbox = Driver.findElement(By.xpath("//input[@placeholder='Search for a medication...']"));
WebElementWait wait = new WebElementWait(listbox, 1);
if(waitForPresenceOfElement(listbox, 20)){
type(value);
listbox.sendKeys(Keys.DOWN, Keys.RETURN);
}
} */
5th try
public void selectMedication (String first_3_letters, String text) throws Throwable {
//String text2 = null;
By medication_box = By.xpath("//input[@placeholder='Search for a medication...']");
if(waitForElementPresent(medication_box, 20)) {
selectBySendkeys(medication_box,first_3_letters, text);
//moveToElementAndClick();
emanager.Pass("Enter value in ':' text field", "Entered in field");
} else {
emanager.Failure("Enter value in ':'", "Unable to enter in field", Driver);
}
if(waitForElementPresent(medication_box, 20)) {
selectBySendkeys(medication_box,first_3_letters, text);
//moveToElementAndClick();
6th try:
public void selectmed() {
WebElement medSearch = Driver.findElement(By.xpath("//input[@placeholder='Search for a medication...']"));
Select dropdownXpath = new Select(medSearch);
List<WebElement>dropdown_list = dropdownXpath.getOptions();
int total_rx = dropdown_list.size();
System.out.println("Totalrx" +total_rx);
for(WebElement ele:dropdown_list) {
String rx_name = ele.getText();
System.out.println("rx list" + rx_name);
}
if(waitForPresenceOfElement(medSearch, 20)){
type(medSearch,value);
//moveToElementAndClick(medSearch);
sendKeys(Keys.ArrowDown);
}
7th try:
public void selectFromDropdown(String value) throws IOException, AWTException {
WebElement dropdown = Driver.findElement(By.xpath("//input[@placeholder='Search for a medication...']"));
Select dropdownXpath = new Select(dropdown);
List<WebElement>dropdown_list = dropdownXpath.getOptions();
int total_rx = dropdown_list.size();
System.out.println("Totalrx" +total_rx);
for(WebElement ele:dropdown_list) {
String rx_name = ele.getText();
System.out.println("rx list" + rx_name);
}
System.out.println("Selected " + value + " for Dropdown ");
if(dropdown.isDisplayed()) {
emanager.Pass("Verify whether user is able to select value from dropdown","User is able to select value from dropdown");
System.out.println("User is able to select value from dropdown");
dropdownXpath.selectByVisibleText(value);
} else {
emanager.Failure("Verify whether user is able to select value from dropdown", "User is not able to select value from dropdown", Driver);
System.out.println("User is not able to select value from dropdown");
assert(true);
}
}
这是HTML
<input _ngcontent-c0="" autocapitalize="off" autocomplete="off" autocorrect="off" class="completer-input form-control mbe-input ng-pristine ng-valid ng-touched" ctrinput="" type="search" name="" placeholder="Search for a medication..." maxlength="524288" tabindex="0" xpath="1">
如果我使用这条语句,我会收到错误信息 Select dropdownXpath = new Select(dropdown);错误看起来像这样:“使用选择输入类型”对于它能够输入的所有其他方法,但不是从下拉列表中选择,只是忽略它并继续前进到下一个没有错误的方法
最佳答案
尝试这 2 个选项。
#1 - 假设搜索结果在 DOM 上,您可以进行匹配搜索结果文本的盲 xpath 搜索并选择元素,
WebElement SearchInput = driver.findElement(By.xpath("//input[@type='search']"));
SearchInput.sendKeys("tett");
Thread.sleep(2000);// remove this once you determine how to dynamically wait
WebElement SearchResult = driver.findElement(By.xpath("//*[contains(text(),'TETTERINE')]"));
SearchResult.click();
#2 - 只需使用 Arrown_Down 键即可浏览搜索结果
SearchInput.sendKeys("tett");
SearchInput.sendKeys(Keys.ARROW_DOWN);// move down the results list
SearchInput.sendKeys(Keys.ENTER);
关于java - 无法从搜索框中的动态类型中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57400247/