Error when attempting to move selenium using headlesschrome on lambda

Asked 2 years ago, Updated 2 years ago, 63 views

It's multi-post.
https://teratail.com/questions/375966

I also asked for answers on teratail, but
I will also post it here as it does not seem to solve the problem.

I'd like to use headlesschrome on lambda and use selenium.
It's not working.

I used this as a reference to build the environment.
The version is the same as this one.
https://qiita.com/ichihara-development/items/5e61c3424b3176bc6096

It works locally, but
I get an error trying to run on lambda.

It looks like the send_keys method is not working.

By the way, the local is Python 2.7.16 and
Lambda is Python 3.7.

When I looked into various things,
I've seen some articles saying that set_keys are not compatible because the chrome-driver version is older.
Currently, the headless-chrominium version is not keeping up with the latest chrome version, so
I wonder if it is impossible for them to deal with it.
(The local chrome version is up to date, so does it work with the chrome-driver version?)

I switched the local Python version to 3 series (3.7.3) combined with lambda side and ran it.
It worked well locally.

If you know anything, please take care of me.

Error Message

Test Event Name
selenium

Response
{
  "errorMessage": "'dict' object has no attribute's send_keys',
  "errorType": "AttributeError",
  "stackTrace": [
    "  File\"/var/task/lambda_function.py\", line 39, in lambda_handler\ndriver.find_element_by_name(\"user_password\").send_keys(\"XXXXXX\")\n"
  ]
}

Function Logs
START RequestId:XXX Version: $LATEST
[ERROR] AttributeError: 'dict' object has no attribute' send_keys'
Traceback (most recent call last):
  File"/var/task/lambda_function.py", line 39, in lambda_handler
    driver.find_element_by_name("user_password").send_keys("XXXXX")
END RequestId: XXX
REPORT RequestId:XXX Duration: 39112.18 ms Built Duration: 39113 ms Memory Size: 256 MB Max Memory Used: 256 MB Init Duration: 301.10 ms

Request ID
a68d20b9-adcd-48a9-ac2a-3a031fcae879

Code (Python)

#-*-coding:utf-8-*-

import time
import subprocess
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import requests
import json
from selenium.webdriver.common.keys import Keys

deflambda_handler(event, context):

    options=webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    options.add_argument("--single-process")
    options.add_argument("--ignore-certificate-errors")
    options.add_argument("--no-sandbox")
    options.add_argument("--homedir=/tmp")
    options.binary_location="/opt/headless/python/bin/headless-chromium"

    # Browser Definitions
    driver = webdriver.Chrome(
        "/opt/headless/python/bin/chromedriver",
        options=options
    )


    # Go to target site
    driver.get('https://test')

    time.sleep(3)

    # Username
    driver.find_element_by_name("user_id").send_keys("XXXXX")

    # password
    driver.find_element_by_name("user_password").send_keys("XXXXX")

    driver.quit()

python selenium

2022-09-30 11:23

2 Answers

I saw some articles saying that set_keys is not compatible because the chrome-driver version is old.

The headless-chromium, chromedriver version of the Qiita article that you referred to in building your environment and
Compare to the local headless-chromium, chromedriver version.

The Qiita article covered the headerless-chromium of v1.0.0-37 and the download of chromedriver_linux64 of 2.37

.

$curl-SLhttps://github.com/adieuadieu/serverless-chrome/releases/download/v1.0.0-37/stable-headless-chromium-amazonlinux-2017-03.zip >headless-chromium.zip

$curl-SLhttps://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip >chromedriver.zip

However, the headless-chromium in the repository was v1.0.0-57.
https://github.com/adieuadieu/serverless-chrome/releases/

Chromedriver also has a newer version.
https://chromedriver.chromium.org/downloads


2022-09-30 11:23

I encountered a similar event, but please refer to the URL below.

options.add_experimental_option("w3c", True)

It worked after I configured the .
I don't know if it's exactly the same thing, but just for your information.

https://stackoverflow.com/questions/69582816/python-selenium-attributeerror-dict-object-has-no-attribute-get-attribute

So, it's another option that could lead to this error. You can fix it by removing it or changing it value to True.


2022-09-30 11:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.