我在自動建議下拉選單中有兩個字串,例如 1. qapostgres112axdef 2. qapostgres112
我需要匹配第二個值(qapostgres112)并且需要點擊它。為此,我使用了 contains() 方法。因為,兩個字串都包含相同的字符 'qapostgres112' selenium 對第一個字串執行點擊操作。
driver.findElement(By.xpath("//span[@role=\"combobox\"]")).click();
WebElement project = driver.findElement(By.xpath("//input[@type=\"search\"]"));
project.click();
project.sendKeys("qapostgres112");
List<WebElement> ddown = driver.findElements(By.xpath("//span[@class=\"select2-dropdown select2-dropdown--below\"]"));
for (WebElement element : ddown) {
if (element.getText().contains("qapostgres112"))
element.click();
}
}
uj5u.com熱心網友回復:
由于兩個選項都包含qapostgres112,因此 contains() 方法將選擇第一個部分匹配項。您可以使用 text(),它會檢查完全匹配。
例子:
//tagname[text()='qapostgres112']
uj5u.com熱心網友回復:
您可以使用SelectSelenium 中的類下可用的各種方法從下拉串列中選擇一個值。
selectByVisibleText(字串引數)
Select drpCountry = new Select(driver.findElement(By.name("country")));
drpCountry.selectByVisibleText("ANTARCTICA");
selectByIndex(字串引數)
Select s = new Select(driver.findElement(By.id("<< id exp>>")));
s.selectByIndex(1);
selectByValue(字串引數)
Select s = new Select(driver.findElement(By.id("<< id exp>>")));
s.selectByValue(“Testing”);
我希望這能幫到您。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507726.html
上一篇:如何在此特定按鈕上執行單擊操作?
