0

I've been using an EC2 instance to run a python script with cron everyday for a month or so. The script uses selenium. Everything was working correctly until today, when my script did not run.

I have tried to run it manually but it's not working either. The error message says that

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.
NoSuchElementException: Message: no such element: Unable to locate element: 
{"method":"cssselector","selector":"#ctl00_ctl00_moteurRapideOffre_
ctl01_EngineCriteriaCollection_Contract > option:nth-child(5)"}
(Session info: headless chrome=90.0.4430.85)

However, the same script is running fine on my computer (ie on my Macbook, not on AWS EC2).

As the problem seems to come from Chrome, I uninstalled it on AWS EC2 using:

sudo yum remove google-chrome-stable 

Then I reinstalled it using :

curl https://intoli.com/install-google-chrome.sh | bash
sudo mv /usr/bin/google-chrome-stable /usr/bin/google-chrome
google-chrome --version && which google-chrome

If I try to run Chrome on the EC2 using /usr/bin/google-chrome, it does not work and it displays the following error message :

ERROR:browser_main_loop.cc(1386)] Unable to open X display.

I don't know if it was working before as I have never used it this way. But it seems to be a problem.

I have seen on the web that it might come from the fact that there is no screen and that I should use a package named xvfb. I have tried to install it with the following code:

sudo yum install xorg-x11-server-Xvfb

I guess the package was correclty installed, but it is not working better.

To sum up, I think my problem in the python code is linked to the fact that Google Chrome is not working correclty and this might be linked to xvfb. But I am not sure at all, it is just what I have tried until now.

Could you please help me ? Thanks!

TheSeacfr
  • 1
  • 1
  • I have a few questions. 1) What exactly is the error you got at the first time? Would be better if you copied and then pasted it in the question body. 2) Now you are getting a whole different error than the first one, is that so? 3) Were you able to successfully install the package xvfb, because you say 'it is not working better' and nothing about if xvfb is working or not? Would be better if you update your answer according to the above questions. That would help the others to understand your question better. – Sandun Apr 27 '21 at 10:50
  • Thank you for your answer, I have tried to modify my question according to your suggestions! Regarding xvfb, I don't really know how to check if it is working correctly or not. I can only say that there was no error message when I entered the command to install it. – TheSeacfr May 01 '21 at 16:18

1 Answers1

0

You can simply add setup your like this, runs after every 30 minutes

*/30 * * * * export DISPLAY=:0 && ,<do what ever you want.>

If this does not work, and you google-chrome or firefox not found, simply run the command below in your shell BASH, FISH, ZSH etc to get PATH.

echo $PATH

Whatever the result comes out from the above command just copy and paste it above your cronjob like this,

*/30 * * * * export DISPLAY=:0 && ,<your selenium script.>```

You can remove export ```export DISPLAY=:0``` line if you want to this in the background or make your driver headless.

The reason of doing this, you might install the respective from snapd etc and that's why path is not defined as you downloaded from separate resource.