L
L
Lim_Drake2020-11-22 18:01:07
Python
Lim_Drake, 2020-11-22 18:01:07

How to add AdBlock ad blocker in Selenium webdriver?

Hello, when parsing, I had a task - to collect customer phone numbers. I do this using Selenium chromedriver, as I need to press a button to make the phone appear. I coped with this task, the parser works, but I have a problem that due to pop-up ads on the site, Selenium sometimes cannot click on the button.
I found out that it is possible to integrate AdBlock into Selenium. I tried looking for documentation but didn't find anything.
I would like to receive links to resources where I can learn more about this Selenium function.
Since I still met similar questions on the net, and there were some solutions: here!
I also have a question, where can I get the AdBlock files themselves in order to use them as in the example?
Is it possible to use extension files from Google Chrome that I found in the browser files, that is, I will specify the path to these files, will this work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-11-22
@Lim_Drake

You can not invent bicycles, but use a permanent profile.
Both options are working 100 pistons !
Tested personally on Linux Mint 19.3 and Windows 10 20H2.
If the profile doesn't exist when you start it, chrome creates it. When you close the profile, it is saved and picks it up with all the settings and extensions the next time you start it.
Code (for Linux):

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("user-data-dir=/home/sergey/SeleniumProfile")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://google.com")

Run it, it creates a profile. Next, in the browser, go to the store with extensions and install any extensions for yourself. All! The next time you run the code, selenium will use this profile along with the extensions.
5fba809595a29657435273.png
Here is the code for Windows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("user-data-dir=C:\\profile")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://google.com")

5fba9f0f92a7f243822459.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question