如何用 Java 编写 Selenium WebDriver 脚本来与此 Ionic Angular 下拉菜单交互?
我想单击下拉菜单并从列表中选择一个元素。 下拉列表中的选项是“经理”和“受训者”。
下拉菜单的角度代码:
<ion-item mode="md" lines="none" class="fluent-textbox" *ngIf="grantedRoles.length > 1">
<ion-select interface="popover" justify="start" (ionChange)="changeUserRole($event)" placeholder="Sign-in as"
[selectedText]="userRole" name="userRole">
<ion-select-option *ngFor="let grantedRole of grantedRoles; let i = index " style="color: var(--salt-theme-foreground)"
[value]="grantedRole" [name]="grantRoles">{{grantedRole}}
</ion-select-option>
</ion-select>
</ion-item>
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExplicitWaitDropdownExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("your-angular-app-url");
WebElement dropdown = driver.findElement(By.name("userRole"));
dropdown.click();
// Use explicit wait for the dropdown to be clickable
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement option = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//ion-select-option[text()='MANAGER']")));
// Click the desired option
option.click();
} finally {
driver.quit();
}
}
}