英文:
How to close the promotion popup in makemytrip.com using selenium webdriver
问题
我无法关闭https://www.makemytrip.com/网站上的以下窗口:
我尝试使用alert
、notification
和子浏览器弹出窗口,但都没有成功。
请有人帮助吗?
英文:
I am not able to close the below window on https://www.makemytrip.com/ website:
I tried with alert
, notification
and child browser popup, but nothing had worked out.
Please anyone help on this?
答案1
得分: 1
要关闭弹出窗口的对象(关闭按钮)位于另一个框架内。
切换到该iframe,然后尝试点击关闭按钮。请查看以下切换框架的代码。
driver.switchTo().frame("元素的ID");
英文:
The object(close button) you should have hit to close the pop up is inside another frame.
So switch to that iframe first and then try to hit the close button. See below the code to switch frame.
driver.switchTo().frame("id of the element");
答案2
得分: 1
点击元素<kbd>X</kbd>,因为所需元素位于<iframe>
中,所以你需要:
-
使用WebDriverWait来等待所需的_frameToBeAvailableAndSwitchToIt_。
-
使用WebDriverWait来等待所需的_elementToBeClickable_。
-
你可以使用以下任一定位策略之一:
-
使用_cssSelector_:
driver.get("https://www.makemytrip.com/"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title^='notification-frame']"))); wait until(ExpectedConditions.elementToBeClickable(By.cssSelector("i.we_close"))).click();
-
使用_xpath_:
driver.get("https://www.makemytrip.com/"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@title, 'notification-frame')]"))); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//i[@class='wewidgeticon we_close']"))).click();
-
浏览器快照:
参考
你可以在以下讨论中找到一些相关讨论:
- 在Selenium Webdriver Java中是否可以在不使用driver.switchTo().frame(“frameName”)的情况下切换到帧中的元素?
- Selenium:无法单击iframe内的按钮
英文:
To click on the element <kbd>X</kbd> as the desired element is within a <iframe>
so you have to:
-
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
-
Induce WebDriverWait for the desired elementToBeClickable.
-
You can use either of the following Locator Strategies:
-
Using cssSelector:
driver.get("https://www.makemytrip.com/"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title^='notification-frame']"))); wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("i.we_close"))).click();
-
Using xpath:
driver.get("https://www.makemytrip.com/"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@title, 'notification-frame')]"))); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//i[@class='wewidgeticon we_close']"))).click();
-
Browser Snapshot:
References
You can find a couple of relevant discussions in:
答案3
得分: 0
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
// 切换到通知框架
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[starts-with(@title, 'notification-frame')]")));
// 等待关闭按钮可点击
Thread.sleep(12000);
driver.findElement(By.xpath("//i[@class='wewidgeticon we_close']")).click();
// 切换回默认内容
driver.switchTo().defaultContent();
英文:
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
// Switch to the notification iframe
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[starts-with(@title, 'notification-frame')]")));
// Wait for the close button to be clickable
Thread.sleep(12000);
driver.findElement(By.xpath("//i[@class='wewidgeticon we_close']")).click();
// Switch back to the default content
driver.switchTo().defaultContent();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论