我正在尝试自动化一个测试用例,其中文本框提供智能来自动完成该字段。请在下面找到自动填充文本框的链接:qazxsw poi
请找到我写的代码
http://demoqa.com/autocomplete/
此代码无法在浏览器提供的智能中查找和查找选项。请帮忙。
您无法在DOM中找到自动建议的选项元素,因为页面重新加载后这些选项的HTML ID会发生变化。
在这种情况下,您需要使用XPath来标识元素。假设您要单击Java自动建议选项,那么您的代码应该是 -
dr.findElement(By.id("tagss")).sendKeys("a");
Thread.sleep(300);
// dr.findElement(By.id("ui-id-53")).click();
Actions act = new Actions(dr);
act.moveToElement(dr.findElement(By.id("ui-id-53"))).click().build().perform();
使用System.setProperty("webdriver.chrome.driver","C:\\WebDriver\\TestAutomation\\grid\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demoqa.com/autocomplete/");
driver.manage().window().maximize();
driver.findElement(By.id("tagss")).sendKeys("a");
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("ui-id-1"))));
WebElement javaOption = driver.findElement(By.xpath(".//li[@class='ui-menu-item' and text()='Java']"));
javaOption.click();
不是好习惯
希望这能帮助你。
因为您正在使用可能在运行时更改的动态ID。试试这个代码并告诉我任何疑问 -
Thread.sleep();