java 如何使用Selenium“点击”来自Amazon上的自我建议的某个建议?

xn1cxnb4  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(92)

我尝试在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;
bxjv4tth

bxjv4tth1#

使用WebdriverWait处理动态元素,并使用以下xpath

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@data-keyword='turbotax']")));
element.click()

相关问题