Docker getting Selenium to run - python

I'm trying to run a python script with Selenium inside docker. Since the script is in a docker container, I keep getting this error:
selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
executable needs to be in PATH.
The code works just fine, when I run it on my computer and use the PATH to the chromedriver file, however a docker container can't see the PATH as it is isolated. I would use a pre-made docker image, however I need other dependencies for my code to run such as smtplib, imapclient, and beautifulsoup to name a few.
Is there a command that will tell Selenium that the chromedriver is in the same folder as the script, or some other way to make it visible to docker?

Docker container is a like a VM. Inside the image you have scripts. But not chromedriver. I would say do not copy the chromedriver inside the image. Just copying the chromedriver alone will not work.
Instead use selenium/node-chrome image and give the container name as the host to your container.
Take a look at this example.
http://www.testautomationguru.com/selenium-webdriver-how-to-run-multiple-test-suites-using-docker-compose/

You could use the following code snippet to set the path to the chrome driver within your script:
import os
path_to_chrome_driver = os.path.join(os.getcwd(), 'chromedriver.exe')
browser = webdriver.Chrome(executable_path=path_to_chrome_driver )

Related

How do I open chrome on the host from a python selenium script running in a docker container?

I need to debug a python script that run in a Docker container. I want to run the chrome driver in a NOT headless mode to see what is happening, but obviously the window of the browser cannot be opened in the container.
Is there a way to open the Chrome on the host machine (my macbook)?
Something like telling Selenium to use directly the Chrome on my host instead of the one installed on the container.
If you are using the docker-selenium image, there is a debugging section which explains how to do what you want. https://github.com/SeleniumHQ/docker-selenium#debugging
It basically involves starting a VNC server in the docker container which is running selenium. Which could also be an option even if you are not using the docker-selenium image.

where to place the chromedriver in aws lambda

I am working in a web scraping project with python 3.7.
Done the code in python with selenium and chromediver.exe in windows, it's working fine.
we are adding the script in aws lambda.
the issue is we need to specify the chrome driver of Linux.
I followed the steps in https://github.com/yai333/Selenium-UI-testing-with-AWS-Lambda-Layers.
i am not using any serverless yml sript(i don't know the same).
doing the following
we have a Linux machine.
create a virtual python environment and add selenium module(as described in awshttps://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html#python-package-venv)
download chromdriver and headless into a folder(size is large, so upload to S3) add both (chrome driver and python lib) as layers.
paste the handler.py (in https://github.com/yai333/Selenium-UI-testing-with-AWS-Lambda-Layers) to a lambdahandler file.
create a sample test, and click on test.
shows error:
error message 'chromedriver' executable needs to be in path
can I upload chrome driver in S3 and show the path.
Having just fought this exact issue for a few hours, I think I can help.
Within your Lambda Layer, you need to include the chromedriver binary under /bin. Os it will look something like:
layerZip/
|- bin/
|- chromedriver
Within your lambda function's infrastructure, it will exist at /opt/bin/chromedriver. As such, we need to point our Python towards that as the executable. To get it to work, I had to add the following:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = "/opt/bin/chromedriver"
driver = webdriver.Chrome(executable_path="opt/bin/chromedriver", options=chrome_options)

how to open a web browser inside azure function app running on linux with python code

i have created an azure function app with a python code. my code is going to scrape tripadvisor page so i used Selenium to open browser and get the source code for a tripadvisor page. Locally on my machine every thing work fine but on azure the function app that i created can't open the web browser.
the error that i get is 'geckodriver' executable may have wrong permissions.
I tried changing permission like this
but the same probleme happen again
AppInsightsScraper.my_logger.info('opening browser')
cmd = 'sudo chmod 777 "/home/site/wwwroot/geckodriver"'
AppInsightsScraper.my_logger.info(os.system(cmd))
checkCmd = 'ls -l'
AppInsightsScraper.my_logger.info(os.system(checkCmd))
AppInsightsScraper.my_logger.info('test modif permission')
browser = webdriver.Firefox(executable_path='/home/site/wwwroot/geckodriver')
AppInsightsScraper.my_logger.info('browser opened')
the expected result is to open the browser and continue the execution of my code but i get permission error on azure portal 'geckodriver' executable may have wrong permissions.
enter image description here
i can't even understand what 32512 and 0 means in the log informations
The error possibly means that geckodriver isn't on the path so it can't do what you want it to do. I received a similar error for Chromedriver on one of my Django app services on Azure and after hours of searching for solutions online, I'm afraid I have bad news for you.
Azure won't allow you to install custom software which is required for your app to run (firefox for geckodriver, chrome for chromedriver etc)
Even if somehow magically there's firefox available, you will have to specify environment variables for your geckodriver and firefox, which is another hectic task on azure.
But there's an alternative if you are okay to deploy somewhere else instead of Azure (Heroku worked for me). You can easily add the firefox binary and geckodriver buildpack and set the path variables and it'll run like a charm. Check this tutorial for the same, I hope it helps.

So when I run my python selenium script through jenkins, how should I write the 'driver = webdriver.Chrome()'?

So when I run my python selenium script through Jenkins, how should I write the driver = webdriver.Chrome()
How should I put the chrome webdriver EXE in jenkins?
Where should I put it?
If you have added your repository path in jenkins during job configuration, Jenkins will create a virtual copy of your workspace. So, as long as the webdriver file is somewhere in your project folder structure and as long as you are using relative path to reference it in your code, there shouldn't be any issues with respect to driver in invocation.
You question also depends on several params like:
1. Whether you are using Maven to run the test
2. Whether you are running tests on Jenkins locally or on a remote machine using Selenium Grid Architecture.

How to access local files with chrome (--allow-access doesn't work)

I am running a python script for a webpage. The python script connects to localhost:5000. In a html I have some images that are in local storage I want to load.
I have set the file to src to file:///path/to/file/some_image.jpg
However I keep getting the error Not allowed to load local resource
I have tried to allow local access using:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files
However I get the same error. I have tried restarting my computer, chrome and ran everything from admin but still no luck.
How can I run local files in chrome with localhost?
Well, have you checked whether you have permission to access those files?
Maybe you can try to use selenium to control the chrome through python, below is a simple python code that you can check it out(Replace the google.com to your html file).
http://selenium-python.readthedocs.io/installation.html#introduction
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--allow-file-access-from-files")
driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", chrome_options=options)
driver.get("http://www.google.com")

Categories

Resources