【发布时间】: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