关闭具有动态ID的广告

问题描述 投票:0回答:1

首先,我想为我的代码看起来缺乏可用的示例而道歉。不幸的是,它包含访问我的大学可用数据库的个人信息,因此我无法发布。数据库的链接在这里:http://www.studiolegale.leggiditalia.it/#mode=home,__m=site

现在,如果您没有合适的广告拦截器,您会看到一个垃圾邮件弹出窗口。我正在使用Selenium在python中构建一个scraper,问题似乎是我无法找到关闭该窗口的方法。我需要为了我的项目而摆脱它,但我不知道如何。我尝试创建一个动作链来定位并单击“X”按钮,但XPath和所述按钮的ID都是动态的。

然后我注意到X按钮的ID总是包含'close',所以我尝试写了类似的东西

spamlist = browser.find_element_by_xpath('//div[contains(@id, "close")] ')
spamlist.click

但无济于事。它没有给我任何错误,但它没有关闭窗口。我承认我是一个初学者,但我真的想不出我做错了什么。有什么明显的东西让我失踪吗?

编辑1.1我被要求提供'关闭元素'按钮的HTML代码。这里是:

<div id="id70a13b6c7dclose70a13b6c7d" style="display: block !important; position: absolute !important; z-index: 10 !important; height: 70px !important; padding: 20px !important; margin-top: -20px !important; margin-left: -20px !important; box-sizing: border-box !important; left: 12px; right: auto; top: 12px; bottom: auto; transform: none; transform-origin: left top;"><div id="id70a13b6c7dclose-cross70a13b6c7d" style="display:block !important;height:30px !important;min-width:30px !important;background:#000 !important;border-radius:5px !important;border:2px solid #fff !important;white-space:nowrap !important;position:relative !important;-webkit-box-sizing:border-box !important;box-sizing:border-box !important;"><div id="id70a13b6c7dcross-container70a13b6c7d" style="overflow:visible !important;-webkit-box-sizing:border-box !important;box-sizing:border-box !important;"><div id="id70a13b6c7dcross70a13b6c7d" style="position:absolute !important;height:20px !important;width:20px !important;top:3px !important;left:3px !important;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOAgMAAABiJsVCAAAADFBMVEUAAAD///////////84wDuoAAAAA3RSTlMAgGJiSVzhAAAAOElEQVQI12PQYGBqYNjLwP6AYb+D/AMG+Qv1DgzsX/8yMDD+/8LAwFB/AUxAuGAJsBKwYrA2sAEADzMWbZHdzhcAAAAASUVORK5CYII=) !important;background:url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTQiIGhlaWdodD0iMTQiIHZpZXdCb3g9IjAgMCAxNCAxNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNLjcgMi43Yy0uNC0uNC0uNC0xIDAtMS40bC42LS42Yy40LS40IDEtLjQgMS40IDBMNyA1IDExLjMuN2MuNC0uNCAxLS40IDEuNCAwbC42LjZjLjQuNC40IDEgMCAxLjRMOSA3bDQuMyA0LjNjLjQuNC40IDEgMCAxLjRsLS42LjZjLS40LjQtMSAuNC0xLjQgMEw3IDlsLTQuMyA0LjNjLS40LjQtMSAuNC0xLjQgMGwtLjYtLjZjLS40LS40LS40LTEgMC0xLjRMNSA3IC43IDIuN3oiIGZpbGw9IiNGRkYiIGZpbGwtcnVsZT0iZXZlbm9kZCIvPjwvc3ZnPg==), linear-gradient(transparent, transparent) !important;background-size:20px !important;overflow:visible !important;-webkit-box-sizing:border-box !important;box-sizing:border-box !important;"></div></div><div id="id70a13b6c7dcounter70a13b6c7d" style="display:none !important;text-align:center !important;position:absolute !important;min-height:30px !important;min-width:30px !important;color:#fff !important;font:small-caps bold 18px/30px courier new, sans-serif !important;overflow:visible !important;-webkit-box-sizing:border-box !important;box-sizing:border-box !important;"></div></div></div>
python-3.x selenium selenium-webdriver web-scraping selenium-chromedriver
1个回答
-1
投票

有可能弹出窗口在iframe中。所以你需要在关闭弹出窗口之前切换到框架。

Driver.SwitchTo().frame("frame xpath");

Driver.findElement("enter your xpath to close popup").click();

Driver.SwitchTo().defaultContent();
© www.soinside.com 2019 - 2024. All rights reserved.