【问题标题】:Selenium - bypass ads Google_VignetteSelenium - 绕过广告 Google_Vignette
【发布时间】:2022-10-01 12:31:02
【问题描述】:

我正在尝试抓取网站并遇到谷歌广告。我想我已经找到了它的 iframe,但我找不到要点击以删除广告的元素。我现在花了大约 7 个小时,认为这超出了我的想象。帮助 v 非常感谢。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_argument(\"--incognito\")
chrome_options.add_argument(\"--window-size=1920x1080\")
# chrome_options.add_argument(\"--headless\")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path =\'C:\\/Users\\/gblac\\/OneDrive\\/Desktop\\/Chromedriver.exe\')

url = \'https://free-mp3-download.net/\'
driver.get(url)

WebDriverWait(driver, 4)
search = driver.find_element(By.ID,\'q\')
search.send_keys(\'testing songs\')
search.click()

button = driver.find_element(By.ID,\'snd\')
button.click()

WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,\'container\'))).click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID,\"results_t\")));
results = driver.find_element(By.ID,\'results_t\').find_elements(By.TAG_NAME,\'tr\')
results[0].find_element(By.TAG_NAME,\'a\').click()
# The code to remove the ad would go here 
# driver.find_elements(By.CSS_SELECTOR,\'[text()=\"Close\"]\').click()
  • 您可以删除用于显示广告的 div
  • 在初始化驱动程序/浏览器时添加一个扩展,如 ublock origin。

标签: python html selenium selenium-webdriver iframe


【解决方案1】:

在您的代码中添加以下代码块 - 在搜索任何文本之前:

time.sleep(1)
driver.execute_script("""
const elements = document.getElementsByClassName("google-auto-placed");
while (elements.length > 0) elements[0].remove();
                      """)

time.sleep(1)
driver.execute_script("""
const elements = document.getElementsByClassName("adsbygoogle adsbygoogle-noablate");
while (elements.length > 0) elements[0].remove();
                      """)

time.sleep(1)
driver.find_element(By.ID,"q").send_keys("tamil songs")
driver.find_element(By.ID,"snd").click()

它将关闭该页面中的 2 个广告块,但是如果您刷新或向前和向后移动,广告将再次显示,那么您必须使用上述代码再次删除这些广告块,请添加该条件的代码。

WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,'container'))).click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID,"results_t")))
results = driver.find_element(By.ID,'results_t').find_elements(By.TAG_NAME,'tr')
results[0].find_element(By.TAG_NAME,'a').click()
time.sleep(2)
driver.find_element(By.XPATH, ".//button[contains(text(),'Download')]").click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
time.sleep(1)

# handling captcha
iframe_captcha = driver.find_element(By.XPATH,".//iframe[@title='reCAPTCHA']")
driver.switch_to.frame(iframe_captcha)
time.sleep(1)
driver.find_element(By.CSS_SELECTOR, ".recaptcha-checkbox-border").click()
time.sleep(2)
driver.switch_to.default_content()

driver.find_element(By.XPATH, ".//button[contains(text(),'Download')]").click()

【讨论】:

  • 谢谢!这完美地工作。
  • 呃,现在我找不到下载按钮了。 @abisaran
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2020-04-03
  • 2022-09-27
  • 1970-01-01
  • 2021-08-05
  • 2021-01-28
相关资源
最近更新 更多