我尝试在amazon.com上自动完成建议。但与google搜索选项不同,建议的xpath总是在变化。我发布的代码并不是每次都能工作,因为有时所需建议的xpath/id/cssselector在变化(@id=\“issDiv 8"),有时是“issDiv 4”或“issDiv 6”等等。
WebElement searchTextField = driver.findElement(By.id("twotabsearchtextbox"));
searchTextField.sendKeys("turbo");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@id=\"issDiv8\"]")));
List<WebElement> autoSuggest = driver.findElements(By.xpath("//*[@id=\"issDiv8\"]"));
System.out.println("Auto Suggest List ::" + autoSuggest.size());
for (int i = 0; i < autoSuggest.size(); i++) {
System.out.println(autoSuggest.get(i).getText());
if (autoSuggest.get(i).getText().equals("turbotax")) {
autoSuggest.get(i).click();
System.out.println("Success");
break;
1条答案
按热度按时间bxjv4tth1#
使用
WebdriverWait
处理动态元素,并使用以下xpath