我有几个 Selenide 自动化测试网站,其中有 Google Vignette。谷歌小插图有不同的 iframe id。由于不同的 google vignette 按钮和不同的 iframe id,我无法使用自动化代码关闭它。
这里的解决方案是什么?有人建议遍历blur() 样式,但是如何呢?还有其他解决方案吗?谢谢。
您可以通过 JS 脚本等待重试 Google 广告,如果出现则将其从 DOM 中删除。
Google Ads 定位器看起来像
[id*=google_ads_iframe],[id*=ad_iframe]
因此,您可以在此元素上调用
querySelector
。如果存在 - 删除它,否则 1 秒后重试。
function waitForElementAndRemove() {
let element = document.querySelector('[id*=google_ads_iframe],[id*=ad_iframe]');
if (element) {
element.remove();
console.log('Removed ad');
} else {
setTimeout(waitForElementAndRemove, 1000);
}
}
waitForElementAndRemove();
public void removeElementAsync(String cssLocator) {
String observerScript = String.format("function waitForElementAndRemove() {" +
" let element = document.querySelector('%s');" +
" if (element) {" +
" element.remove();" +
" console.log('Removed ad');" +
" } else {" +
" setTimeout(waitForElementAndRemove, 1000);" +
" }" +
"}" +
"waitForElementAndRemove();", cssLocator);
Selenide.executeJavaScript(observerScript);
}
使用谷歌小插图测试资源
Selenide.open("https://demo.guru99.com/articles_popup.php#google_vignette");
String adsSelector = "[id*=google_ads_iframe],[id*=ad_iframe]";
removeElementAsync(adsSelector);
ElementsCollection elements = $$(".dropdown a").filter(Condition.visible);
for (int i = 1; i < 5; i++) {
elements.get(i).click();
removeElementAsync(adsSelector);
Selenide.sleep(1000);
}