有没有一种方法可以使用硒来阻止/关闭iframe?

时间:2019-02-14 14:58:04

标签: 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()

这不起作用,我还尝试将XPath设置为广告上的X按钮,但是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)

我希望代码可以从url下载图片,但是我得到了:

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: 

网站(关闭广告块以查看添加)https://downloadgram.com/

1 个答案:

答案 0 :(得分:1)

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

city_in_country(amman, jordan).    
city_in_country(shanghai, china).    
city_in_country(beijing, china).    
city_in_country(brussels, belgium).    
city_in_country(london, uk).    
city_in_country(manchester, uk).

city_visited(amman).    
city_visited(beijing).    
city_visited(shanghai).    
city_visited(cairo).    

countries_visited(ThisCountry, ThisCity) :- 
    city_visited(ThisCity), 
    city_in_country(ThisCity, ThisCountry).

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