如何使用python中的selenium从网页中删除广告

2024-09-28 22:18:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用selenium搜索机场列表,以便能够从我使用tab访问的网站获取信息。每次当广告没有出现在网页上时,它都会起作用。我花了一点时间试图解决这个问题,但似乎没有任何效果。这是我的代码的一小部分,你们可以理解我在做什么:

    driver = webdriver.Chrome()
    driver.get("https://www.google.com")
    a = driver.find_element_by_css_selector(".gLFyf")
    a.clear()
    a.send_keys(f'site:airnav.com/airport airnav {a[i]} {b[i]}')
    print(f'airnav {a[i]} {b[i]}')
    a.send_keys(Keys.RETURN)
    actions = ActionChains(driver)
    actions1 = actions.send_keys(Keys.TAB*19)
    actions1.perform()
    time.sleep(3)
    actions = ActionChains(driver)
    actions2 = actions.send_keys(Keys.ENTER)
    actions2.perform()
    act = driver.current_url

    driver = webdriver.Chrome()
    driver.get(f'https://web.archive.org/web/2015/{act}')
    time.sleep(3)

a和b是列表,我用谷歌搜索这些列表中的每一个元素,并访问同一个网站。唯一让TAB方法停止工作的是,每当我尝试这个方法时,这些广告就会持续出现


Tags: httpscomsendactions列表get网站driver
1条回答
网友
1楼 · 发布于 2024-09-28 22:18:53

禁用Chrome中的弹出窗口

对于Chrome,默认情况下会启用弹出窗口,即默认情况下禁用弹出窗口阻止程序。要启用弹出窗口阻止程序,即阻止弹出窗口,请在chromeOptions功能的ExcludeSwitchs下传递disable popup blocking参数,如下所示:

代码:

options = webdriver.ChromeOptions()
capabilities = options.to_capabilities()
capabilities = {
 'browser': 'chrome',
 'browser_version': 'latest',
 'os': 'Windows',
 'os_version': '10',
 'build': 'Python Sample Build',
 'name': 'Pop-ups testing'
}
capabilities["chromeOptions"]["excludeSwitches"] = ["disable-popup-blocking"]
#options.add_argument(" headless")
driver = webdriver.Chrome("C:\\Users\\etc\\Desktop\\Selenium+Python\\chromedriver.exe", options=options)
driver.get("Your URL")

相关问题 更多 >