Reputation: 49
I am trying to disable AdBlock for a specific website only, but I can't find a way to do it. I tried looking in the selenium documentation, but I couldn't find any methods to disable extensions afterward. However, I am still pretty new at reading documentation, so I may have missed something. I also tried to automate the disabling of the AdBlock extension by using selenium but it didn't work. The plan was to go to the extension section of chrome(chrome://extensions/), get the "enabled" checkbox and click it without my intervention. Here is my attempt:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException
def main():
opening = True
while opening:
try:
chrome_options = Options()
#Path to AdBlock
chrome_options.add_extension('/usr/local/bin/AdBlock_v.crx')
driver = webdriver.Chrome(chrome_options=chrome_options)
except:
print('An unkown error has occured. Trying again...')
else:
opening = False
disable_adblocker(driver)
def click_element(driver, xpath, index):
getting = True
not_found_times = 0
while getting:
try:
getting = False
element = WebDriverWait(driver, 5).until(
EC.presence_of_all_elements_located((By.XPATH,xpath)))[index]
element.click()
#driver.get(element.get_attribute("href"))
#In case the page does not load properly
except TimeoutException:
not_found_times += 1
if not_found_times < 2:
driver.refresh()
getting = True
else:
raise
#In case DOM updates which makes elements stale
except StaleElementReferenceException:
getting = True
def disable_adblocker(driver):
driver.get('chrome://extensions')
ad_blocker_xpath = '//div[@id="gighmmpiobklfepjocnamgkkbiglidom"]//div[@class="enable-controls"]//input'
click_element(driver,ad_blocker_xpath,0)
print('')
main()
The reason my attempt failed is because selenium couldn't use the xpath, I specified, to get the checkbox element. I believe the path is correct.
The only solution that I can think of is creating two chrome windows: one with AdBlock and another without AdBlock. However, I don't want two windows as this will make things more complicated.
Upvotes: 0
Views: 1757
Reputation: 574
It doesn't look like this is possible using any settings in selenium. However... You can automate adding the domain you wish to exclude after creating the driver.
Before your test actually starts, but after you've initialized the browser, navigate to chrome-extension://[your AdBlock extention ID]/options.html. AdBlock extension ID is unique to the crx file. So go into chrome and find the value in the extension manager. For example, mine is gighmmpiobklfepjocnamgkkbiglidom.
After you've navigated to that page, click 'Customize', then 'Show ads everywhere except for these domains...', then input the domain into the field, then click 'OK'. Boom! Now the domain is added and will show ads! Just make sure
I know its not the ideal quick, easy, one line of code solution... But it seems like the best option, unless you want to go digging in the local storage files and find where this data is added to...
Upvotes: 1