VicGab
VicGab

Reputation: 5

Blocking downloads on Chrome using Selenium with Python

I made a simple bot on python using selenium that researches a certain content in a website and returns a specific result to the user. However, that website has an ad that every time I click on a page element, or click ENTER to advance (or the bot clicks in this case) it downloads a file. I only found out about this after running a few tests while improving the bot. I tried doing it manually and the same thing happened, so it's a problem with the website itself.

But I'm guessing there's a way to completely block the downloads, because it's saving that file automatically. I don't think it makes that much difference, but this is what triggers the download:

driver.find_element(By.ID, "hero-search").send_keys(Keys.ENTER)

And I can't go around that because I need to advance to the next page. So, is there a way to block this on selenium?

Upvotes: 0

Views: 1396

Answers (1)

art_architect
art_architect

Reputation: 929

You can block the downloads by using the chrome preferences:

from selenium import webdriver

options = webdriver.ChromeOptions()

prefs = {
    "download.prompt_for_download", false,
    "download_restrictions": 3,
}
options.add_experimental_option(
    "prefs", prefs
)
driver = webdriver.Chrome(
    options=options
)
driver.get(
"https://www.an_url.com"
)
driver.close()

Upvotes: 1

Related Questions