如何处理弹出 windows Java Selenium?

How to handle pop up windows Java Selenium?

例如,TjMaxx

当用户第一次导航时,弹出 window 会出现,有时会出现 2 个,而且很难抓住并继续前进。我尝试了 try catch,但它不起作用,因为它打开了一些我看不到的 windows。如何为来宾用户捕获所有 windows 并解散它们?

 try {
        WebElement closeWindowPromo = driver.findElement(By.xpath
                ("//div[@class =\"bx-row bx-row-submit bx-row-submit-no  bx-row-2uaILGf bx-element-1360650-2uaILGf\"]//button[@data-click = 'close']"));
        WebDriverWait w = new WebDriverWait(driver, 5);
        w.until(ExpectedConditions.visibilityOfElementLocated((By) closeWindowPromo));
        System.out.println("Element is visible");

    } catch (NoSuchElementException n) {

        System.out.println("Element is invisible");
    }
    wait = new WebDriverWait(driver, 10);

不要单击 关闭 按钮,而是尝试单击文本为 我不想免费送货的元素 诱导 for the elementToBeClickable() and you can use either of the following :

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[class*='bx-row-submit-no'] > button.bx-button[data-click = 'close'][type='reset']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='bx-button' and @data-click = 'close'][@type='reset' and contains(., 'shipping')]"))).click();