简体   繁体   English

如何在 python 中使用 selenium 从网页中删除广告

[英]How to remove ads from a webpage with selenium in python

I've been trying to use selenium in order to google a list of airports to be able to get information from a website that I use tab to get to.我一直在尝试使用 selenium 来搜索机场列表,以便能够从我使用选项卡访问的网站获取信息。 It works every single time when the ads don't appear on the webpage.当广告没有出现在网页上时,它每次都有效。 I've spent a bit of time trying to solve this but nothing is seeming to work.我花了一些时间试图解决这个问题,但似乎没有任何效果。 This is a fraction of my code for you guys to understand what I'm trying to do:这是我的代码的一小部分,供你们理解我正在尝试做的事情:

    driver = webdriver.Chrome()
    driver.get("https://www.google.com")
    a = driver.find_element_by_css_selector(".gLFyf")
    a.clear()
    a.send_keys(f'site:airnav.com/airport airnav {a[i]} {b[i]}')
    print(f'airnav {a[i]} {b[i]}')
    a.send_keys(Keys.RETURN)
    actions = ActionChains(driver)
    actions1 = actions.send_keys(Keys.TAB*19)
    actions1.perform()
    time.sleep(3)
    actions = ActionChains(driver)
    actions2 = actions.send_keys(Keys.ENTER)
    actions2.perform()
    act = driver.current_url

    driver = webdriver.Chrome()
    driver.get(f'https://web.archive.org/web/2015/{act}')
    time.sleep(3)

The a and b are lists, I'm googling every single element from those lists and getting to the same website. a 和 b 是列表,我正在搜索这些列表中的每个元素并访问同一个网站。 The only thing that ever stops the TAB method from working are these ads that keep appearing every second time I try this.唯一阻止 TAB 方法工作的是这些广告,每次我尝试此操作时都会不断出现。

Disable pop-ups in Chrome禁用 Chrome 中的弹出窗口

For Chrome, pop-ups are enabled by default ie the pop-up blocker is disabled by default.对于 Chrome,默认情况下会启用弹出窗口,即默认情况下禁用弹出窗口阻止程序。 To enable the pop-up blocker ie to block pop-ups, pass disable-popup-blocking argument under the excludeSwitches of a chromeOptions capability, as shown below:要启用弹出窗口阻止程序,即阻止弹出窗口,请在 chromeOptions 功能的 excludeSwitches 下传递 disable-popup-blocking 参数,如下所示:

Code:代码:

options = webdriver.ChromeOptions()
capabilities = options.to_capabilities()
capabilities = {
 'browser': 'chrome',
 'browser_version': 'latest',
 'os': 'Windows',
 'os_version': '10',
 'build': 'Python Sample Build',
 'name': 'Pop-ups testing'
}
capabilities["chromeOptions"]["excludeSwitches"] = ["disable-popup-blocking"]
#options.add_argument("--headless")
driver = webdriver.Chrome("C:\\Users\\etc\\Desktop\\Selenium+Python\\chromedriver.exe", options=options)
driver.get("Your URL")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM