我正在从 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/