简体   繁体   中英

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. 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. The only thing that ever stops the TAB method from working are these ads that keep appearing every second time I try this.

Disable pop-ups in Chrome

For Chrome, pop-ups are enabled by default ie the pop-up blocker is disabled by default. 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:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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