Java 关闭随机';弹出窗口'; 我已经创建了运行相同时间的WebDriver测试 一些测试显示“PopupX”将在30秒后出现 在某些测试中,“PopupX”将在60秒时出现 在一些测试中,“PopupX”将在35秒时出现
你知道身份证Java 关闭随机';弹出窗口'; 我已经创建了运行相同时间的WebDriver测试 一些测试显示“PopupX”将在30秒后出现 在某些测试中,“PopupX”将在60秒时出现 在一些测试中,“PopupX”将在35秒时出现,java,maven,selenium,selenium-webdriver,Java,Maven,Selenium,Selenium Webdriver,你知道身份证 弹出窗口总是有一个唯一的Id“X”来关闭应用程序,甚至不同的弹出窗口也有相同的“X”关闭选项 是否有人知道一种持续搜索和关闭“一种形式的连续轮询”的方法,如果弹出窗口在任何测试用例中随时出现,该方法将关闭弹出窗口? 我知道下面列出的方法有效,可以成功关闭弹出窗口: public void closeGiveawayPopup() throws InterruptedException { WebDriverWait wait = new WebDriverWait(driver,
public void closeGiveawayPopup() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
try {
List<WebElement> elements = wait
.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(".close")));
for (WebElement element : elements) {
if (element.isDisplayed()) {
element.click();
Thread.sleep(1000);
wait.until(ExpectedConditions.invisibilityOfAllElements(elements));
Thread.sleep(2000);
}
}
} catch (Exception e) {
throw (e);
}
public void closeGiveawayPopup()引发InterruptedException{
WebDriverWait wait=新的WebDriverWait(驱动程序,10);
试一试{
列表元素=等待
.直到(预期条件。被(CSS选择器(“.close”))延迟的所有事件的出现);
for(WebElement:elements){
if(element.isDisplayed()){
元素。单击();
睡眠(1000);
等待.直到(ExpectedConditions.InvisibilityFallElements(elements));
《睡眠》(2000年);
}
}
}捕获(例外e){
投掷(e);
}
public void closeGiveawayPopup() throws Exception {
try {
List<WebElement> elements = getDriver().findElements(By.cssSelector(".close"));
for (WebElement element : elements) {
if (element.isDisplayed()) {
element.click();
}
}
} catch (Exception e) {
throw (e);
}
public void closegiveawaypoup()引发异常{
试一试{
List elements=getDriver().findElements(By.cssSelector(“.close”);
for(WebElement:elements){
if(element.isDisplayed()){
元素。单击();
}
}
}捕获(例外e){
投掷(e);
}
}Phil,我不认为你做错了什么。@BeforeMethod注释是一条路。唯一需要改进的是包括(如果你还没有的话)分别用于浏览器启动/关闭的@BeforeClass注释。也许,这就是您在上面的描述中提到的“浏览器甚至没有打开”的原因。因此类似这样的情况:
@BeforeClass
public void initialSetup(String browser){
Webdriver driver = new browser ();
driver.get("https://www.buyagift.co.uk/");
}
@BeforeMethod
public void closeGiveAwayPopup(){
//same as your code
}
@Test(priority=1)
public void makeSureIarrivedOnTheHomePage(){
titleGrabbed = driver.getTitle().toString();
titleExpected = "Experience Days and Gifts from Buyagift";
Assert.assertEquals(titleGrabbed ,titleExpected )
}
@Test(priority=2)
public void clickLoginButton(){
//more actions
}
@AfterClass
public void shutDown(){
driver.quit();
}
driver.findElement(By.xpath("//div[@id='competiton_giveaway_popup']//button[@id='close']"));
为了达到你所追求的“连续轮询”效果,我能想到的唯一方法就是让你的@Test非常小(一次一步)。这样希望你能达到预期的效果。所以不要在1@Test下捆绑很多WebDriver操作,因为这会增加弹出窗口出现的风险
唯一让我困惑的是你的评论“url甚至没有加载”。你确定你抓住了实际的弹出窗口而没有关闭整个窗口吗
如果是这种情况,或者上述方法不适用于您,请尝试使用Xpath而不是CSS。从屏幕截图中,您需要展开div[@id='competition\u inner\u和outer details'],并在某处找到关闭按钮(可能您正在寻找、或