如何使用Selenium打开Chrome并避免收听DevTools?

时间:2019-02-01 13:40:14

标签: python-3.x selenium-webdriver google-chrome-devtools selenium-chromedriver

因此,这是我关于Stack Overflow的第一篇文章,并且是刚开始编写python 3的新手(无其他脚本背景)。要练习网络抓取,编写脚本来打开浏览器,然后解析网络并非没有挑战。在我迎接挑战的同时,我需要克服一些障碍。

我的代码有两个问题:

1)据报道Chrome Devtools正在监听。

DevTools listening on ws://127.0.0.1:............

2)Chrome浏览器未按预期打开。

Error: Cannot open Chrome browser.


#python3
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent


options = Options()
ua = UserAgent()
user_agent = ua.random
print('Here is the randomly generated UserAgent\n' + str(user_agent))
options.set_headless(headless=True)
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
options.add_argument('user-agent={%s}' % user_agent)
options.add_argument('--log-level=3')
browser = webdriver.Chrome(chrome_options=options)

    try:
        browser.get('https://www.google.com/')
        print('Opening up Chrome to Google\'s homepage URL')

    except:
        print('Error: Cannot open Chrome browser.')

为了避免DevTools听我的网络爬虫,并使用调整后的设置打开Chrome。你能帮我吗?

0 个答案:

没有答案