chrome中的无头模式无法在python

时间:2018-09-12 18:13:18

标签: python selenium google-chrome selenium-webdriver headless

由于某种原因,当我尝试运行无头Chrome时,没有出现此类错误。编辑:请记住,我使用的代理在呈现cnn.com之前会自动通过应用程序的登录页面。

def start_test(proxyHostInput, port):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--incognito')
    chrome_options.add_argument('--proxy-server=%s' % proxyHostInput + port)
    chrome_options.add_argument('--headless')
    chrome_options.add_argument("--window-size=1366x768")
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--remote-debugging-port=3129')
    chromeDriver = r"C:\Users\Surfcrew\Downloads\chromedriver_win32\chromedriver.exe"
    browser = webdriver.Chrome(chromeDriver, chrome_options=chrome_options)
    browser.get('https://www.cnn.com/')

    try:
        # insert username
        username = browser.find_element_by_id("login_username")
        username.send_keys("REDACTED")
        browser.find_element_by_xpath("/html/body[@class='login_body ']/div[@class='topdiv']/div[@id='bar']/span"
                                      "[@id='login_name']/form[@class='form saml']/input[@class='button']").click()
        time.sleep(3)
        password = browser.find_element_by_xpath("/html/body[@class='login_body ']/div[@class='topdiv']/div[@id='bar']"
                                                 "/span[@id='login_password']/form[@class='form password']"
                                                 "/div[@class='formdiv']"
                                                 "/input[@id='password']")

        # insert pass
        password.send_keys(readacted)
        browser.find_element_by_xpath("/html/body[@class='login_body ']/div[@class='topdiv']/div[@id='bar']"
                                      "/span[@id='login_password']/form[@class='form password']"
                                      "/input[@id='password_submit']").click()

但是我仍然收到此错误:

Message: no such element: Unable to locate element: {"method":"id","selector":"login_username"}
  (Session info: headless chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

有人可以解释为什么或如何解决这个问题吗?

0 个答案:

没有答案