如何使用Selenium WebDriver关闭makemytrip.com上的促销弹出窗口

huangapple go评论69阅读模式
英文:

How to close the promotion popup in makemytrip.com using selenium webdriver

问题

我无法关闭https://www.makemytrip.com/网站上的以下窗口:

如何使用Selenium WebDriver关闭makemytrip.com上的促销弹出窗口

我尝试使用alertnotification和子浏览器弹出窗口,但都没有成功。

请有人帮助吗?

英文:

I am not able to close the below window on https://www.makemytrip.com/ website:

如何使用Selenium WebDriver关闭makemytrip.com上的促销弹出窗口

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.

如何使用Selenium WebDriver关闭makemytrip.com上的促销弹出窗口

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>,因为所需元素位于&lt;iframe&gt;中,所以你需要:

  • 使用WebDriverWait来等待所需的_frameToBeAvailableAndSwitchToIt_。

  • 使用WebDriverWait来等待所需的_elementToBeClickable_。

  • 你可以使用以下任一定位策略之一:

  • 使用_cssSelector_:

     driver.get(&quot;https://www.makemytrip.com/&quot;);
     WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
     wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(&quot;iframe[title^=&#39;notification-frame&#39;]&quot;)));
     wait until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;i.we_close&quot;))).click();
    
  • 使用_xpath_:

    driver.get(&quot;https://www.makemytrip.com/&quot;);
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(&quot;//iframe[starts-with(@title, &#39;notification-frame&#39;)]&quot;)));
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//i[@class=&#39;wewidgeticon we_close&#39;]&quot;))).click();
    
  • 浏览器快照:

如何使用Selenium WebDriver关闭makemytrip.com上的促销弹出窗口


参考

你可以在以下讨论中找到一些相关讨论:

英文:

To click on the element <kbd>X</kbd> as the desired element is within a &lt;iframe&gt; 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(&quot;https://www.makemytrip.com/&quot;);
     WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
     wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(&quot;iframe[title^=&#39;notification-frame&#39;]&quot;)));
     wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;i.we_close&quot;))).click();
    
  • Using xpath:

    driver.get(&quot;https://www.makemytrip.com/&quot;);
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(&quot;//iframe[starts-with(@title, &#39;notification-frame&#39;)]&quot;)));
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//i[@class=&#39;wewidgeticon we_close&#39;]&quot;))).click();
    
  • Browser Snapshot:

如何使用Selenium WebDriver关闭makemytrip.com上的促销弹出窗口


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(&quot;https://www.makemytrip.com/&quot;);
	driver.manage().window().maximize();

	// Switch to the notification iframe
	driver.switchTo().frame(driver.findElement(By.xpath(&quot;//iframe[starts-with(@title, &#39;notification-frame&#39;)]&quot;)));

	// Wait for the close button to be clickable
	Thread.sleep(12000);

	driver.findElement(By.xpath(&quot;//i[@class=&#39;wewidgeticon we_close&#39;]&quot;)).click();

	// Switch back to the default content
	driver.switchTo().defaultContent();

huangapple
  • 本文由 发表于 2023年2月19日 18:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定