python - 有没有办法使用 selenium 阻止/关闭 iframe?

标签 python html selenium iframe selenium-chromedriver

我正在从 Instagram 创建一个下载器。该程序从给定主题标签中的热门 Instagram 帖子中获取 URL,并将其输入到下载器中。问题是,当网站首次加载时,弹出式 iframe 广告总是出现在下载按钮上。这会引发无法单击按钮的错误,因为将单击 iframe。

这是针对运行 Chrome 驱动程序的 Python Selenium。我尝试运行一个过滤器来查找 iframe 并返回主页:

all_iframes = self.browser.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
       self.browser.switch_to.default_content()

这不起作用,我也尝试获取广告上X按钮的XPath,但ID每次都会改变,因此无法点击或识别我。

#get the website
        for link in self.links:
            self.browser.get('https://downloadgram.com/')
            time.sleep(5)

#this is the x button click iframe I tried, but the XPath changes
            all_iframes = self.browser.find_elements_by_tag_name("iframe")
            if len(all_iframes) > 0:
                xButton = self.browser.find_element_by_xpath('//div[@id="id3019a64023cross3019a64023"]')
                xButton.click()

#inputs the URL from array links[] into download box
            input = self.browser.find_element_by_xpath('//input[@name="url"]')
            input.clear()
            input.send_keys(link)
            time.sleep(1)

#clicks download button
            download = self.browser.find_element_by_xpath("//input[@type='submit']")
            download.click()
            time.sleep(1)

#clicks confirm button 
            actuallyDownload = self.browser.find_element_by_xpath("//a[@target='_blank']")
            actuallyDownload.click()
            time.sleep(1)

我希望代码下载网址上的图片,但我得到:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input type="submit" value="Download" class="button"> is not clickable at point (451, 446). Other element would receive the click: 

网站(关闭adblock查看添加)https://downloadgram.com/

最佳答案

我可以使用以下代码关闭弹出窗口。请尝试一下。

browser.get('https://downloadgram.com/')
time.sleep(5)

element=browser.find_element_by_xpath("//div[starts-with(@id,'id')]" and "//div[starts-with(@style,'position:absolute !important;height:20px !important;width:20px !important;top:3px !important;left:3px !important;background-image:url(data:image/png;')]")
arrt=element.get_attribute("id") 
print(arrt)
browser.execute_script("arguments[0].click();", element)

让我知道它是否有效。祝你好运。

关于python - 有没有办法使用 selenium 阻止/关闭 iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693304/

相关文章:

python - PhantomJS 1.8 在 python 上使用 Selenium。如何屏蔽图片?

python - 线程1 :EXC_BAD_INSTRACTION

python - 使用python将带有撇号的文件名传递给scp

javascript - 无法阻止点击登录后密码在密码字段中自动延长

python-3.x - selenium.common.exceptions.SessionNotCreatedException : Message: session not created from tab crashed using ChromeDriver Chrome Selenium Python

java - 无法 tp 切换窗口中第三个子 iframe 的上下文

python - 模拟重力作用下的两个粒子

python - 如何在 Odoo-11 中显示警告信息?

html - 如何优化 CSS 动画(缩放、旋转、模糊)

javascript - 我无法让我的标题显示在我的灯箱中