原代码:
def shot(self):
# open in webpage
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# driver = webdriver.Chrome('/usr/local/bin/chromedriver',chrome_options=chrome_options)
driver = webdriver.Chrome('C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\chromedriver.exe',chrome_options=chrome_options)
driver.get(mailfile_path)
driver.save_screenshot(mailshot_path)
driver.quit()
错误原因:
1、chrome和chromedriver版本不一致
报错会提示当前chrome和chromedriver版本
2、driver.get()里面的格式不对,要么url,若打开本地文件,格式应file://xxx
报错 File “/alidata/www/git/auto-api/Common/sendemail.py”, line 75, in shot
driver.get(mailfile_path)
File “/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 333, in get
self.execute(Command.GET, {‘url’: url})
File “/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 321, in execute
self.error_handler.check_response(response)
File “/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py”, line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
解决后代码:
# open in webpage
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('/usr/local/bin/chromedriver',chrome_options=chrome_options)
# driver = webdriver.Chrome('D:\\Python37\\chromedriver.exe',chrome_options=chrome_options)
#打开HTML,需要url地址,或者file://格式,我这里用的是本地的html文件,所以用file://格式
new_path = 'file://'+mailfile_path
driver.get(new_path)
driver.save_screenshot(mailshot_path)
driver.quit()