Selenium, Python and InvalidArgumentException: inv

2020-04-07 05:40发布

问题:

A Python script I have been using for the last several months broke today. The script monitors a modem for incoming calls and files FTC Do Not Call complaints. The error is InvalidArgumentException: Message: invalid argument: value must be a non-negative integer when calling implicitly_wait.

I found similar reports for Firefox, but I have not found one for Chrome. The machine is fully patched, so there is nothing to update like in the Firefox reports.

What is the problem and how do I fix it?


Here is the script:

$ cat test.py
#!/usr/bin/env python3

import sys
import selenium
import os.path

from packaging import version
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def get_chrome():
    if os.path.isfile('/usr/bin/chromium-browser'):
        return '/usr/bin/chromium-browser'
    elif os.path.isfile('/usr/bin/chromium'):
        return '/usr/bin/chromium'
    elif os.path.isfile('/usr/bin/chrome'):
        return '/usr/bin/chrome'
    elif os.path.isfile('/usr/bin/google-chrome'):
        return '/usr/bin/google-chrome'
    else:
        return None

def main():
    opts = Options()
    opts.binary_location = get_chrome()
    opts.add_argument('--headless')
    opts.add_argument('--no-sandbox')
    opts.add_argument('--disable-dev-shm-usage')

    driver = webdriver.Chrome(chrome_options=opts)
    driver.maximize_window()

    agent = driver.execute_script('return navigator.userAgent')
    driver.get("https://complaints.donotcall.gov/complaint/complaintcheck.aspx")
    driver.implicitly_wait(2)
    driver.quit()

if __name__ == "__main__":
    main()

Here is the exception:

$ python3 test.py
Traceback (most recent call last):
  File "test.py", line 39, in <module>
    main()
  File "test.py", line 35, in main
    driver.implicitly_wait(2)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 793, in implicitly_wait
    'implicit': int(float(time_to_wait) * 1000)})
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: value must be a non-negative integer
  (Session info: headless chrome=75.0.3770.90)

Here is the Selenium function from /usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py:

# Timeouts
def implicitly_wait(self, time_to_wait):
    """
    Sets a sticky timeout to implicitly wait for an element to be found,
       or a command to complete. This method only needs to be called one
       time per session. To set the timeout for calls to
       execute_async_script, see set_script_timeout.

    :Args:
     - time_to_wait: Amount of time to wait (in seconds)

    :Usage:
        driver.implicitly_wait(30)
    """
    if self.w3c:
        self.execute(Command.SET_TIMEOUTS, {
            'implicit': int(float(time_to_wait) * 1000)})
    else:
        self.execute(Command.IMPLICIT_WAIT, {
            'ms': float(time_to_wait) * 1000})

Here are the specs. The machine is an ARM dev-board Tritium H3.

$ lsb_release -a
Description:    Ubuntu 18.04.2 LTS
Release:        18.04
Codename:       bionic
...

$ python3 --version
Python 3.6.8

$ chromium-browser --version
Chromium 75.0.3770.90 Built on Ubuntu

$ apt-cache show python3-selenium
Package: python3-selenium
Architecture: all
Version: 3.8.0+dfsg1-3
...

$ apt-cache show chromium-chromedriver
Package: chromium-chromedriver
Architecture: armhf
Version: 75.0.3770.90-0ubuntu0.18.04.1
...