Running Selenium file in Amazon AWS - python

I am running Selenium file on Amazon AWS Ubuntu Server, but I am getting the following error on the below line:
driver = webdriver.Chrome();
The error says:
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127
I have tried many solutions but it is still not working (How to fix Selenium WebDriverException: The browser appears to have exited before we could connect?)
I have also tried with:
driver = webdriver.Firefox();
and in Firefox, I am getting the following error:
selenium.common.exceptions.WebDriverException: Message: connection refused
Can anyone help me?

Since its Amazon AWS please check if Chrome is installed, if not please install from link.

Do you install chromedriver or geckodriver on the server? Your chromedriver should be in all of following directory:
chromedriver: /usr/bin/chromedriver /bin/chromedriver
/usr/local/bin/chromedriver /usr/local/chromedriver
Or, you can paste path directly:
driver = webdriver.Chrome(path='./chromedriver').
Also, use PyVirtualDisplay.

Your problem is due to no display in AWS. When you are trying to execute the command driver = webdriver.Firefox() or driver = webdriver.Chrome() the system tries to launch a Firefox or Chrome browser but because you do not have access to the display in remote access so it is crashing there.
Follow the steps given at this blog and you can create a virtual display to buffer frames. Then your script will not crash due to this problem

Related

Selenium Python Windows "cannot connect to the service"

I've trawled the web and SO for a good few hours and have exhausted all solutions I can find. Windows 10 / Chrome 87.0 / Python 3.
from selenium import webdriver
DRIVER_PATH = r'./chromedriver/chromedriver.exe'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
returns:
WebDriverException: Message: Can not connect to the Service ./chromedriver/chromedriver.exe
I have made sure that the chromedriver is added to path
I have added 127.0.0.1 localhost to a clean hosts file in %windir%\System32\drivers\etc
I have tried specifying the driver path explicitly and implicitly
You need to insert the entire path, on windows its like this:
executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe'
This os path...
r'./chromedriver/chromedriver.exe'
...refers to the ChromeDriver executable within the chromedriver sub-directory which is located in the same directory from where your program executes.
By all possible means r'./chromedriver/chromedriver.exe' is not the actual location of the ChromeDriver executable within your system. Hence you see the error.
Solution
Pass the absolute path of the ChromeDriver as follows:
from selenium import webdriver
DRIVER_PATH = r'C:\..\..\chromedriver\chromedriver.exe'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
References
You can find a couple of relevant detailed discussions in:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python
Python Selenium “Can not connect to the Service %s” % self.path in linux server

Website is not opening in selenium webdriver of google chrome using python

I am trying to open the website in selenium python but it is showing blank page. but when i open that website in normal google chrome it is working .
here is the code i am writing to open the website.
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://shop.coles.com.au/a/wentworth-point/home")
Error I am Getting in chrome console Failed to load resource: the server responded with a status of 429 () and Failed to load resource: net::ERR_UNKNOWN_URL_SCHEME –
You must specify the chromedriver path. Something like this:
https://seleniumbyexamples.github.io/navget
Verify that version of chrome driver is same of chrome browser installed on your machine
and make sure that path of chrome driver is set in your PATH variable.
http://chromedriver.chromium.org/downloads
The error code 429 that you are getting means too many requests are sent in a given amount time. Just try to clean your build history and run it again. Regarding the version of Chrome, if that was to be the issue the browser must have not opened in the first place and you would have got other exception like SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 83
Just to be sure you mat check for the version of chrome you are using and the driver downloaded in the system, if it is not appropriate download the latest chrome driver from web and provide in the executable path to chrome or else add it in environment variables of your system.

Cannot connect to the service chromedriver (Mac) [duplicate]

So I have made a program on one computer using selenium and that worked, Now using it in another computer I get this error:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
Now this same issue was mention in:
Selenium python: Can not connect to the Service %s" % self.path
Selenium python: Can not connect to the Service %s" % self.path
Selenium and Python3 ChromeDriver raises Message: Can not connect to the Service chromedriver
however the solutions mentioned didnt work.
I am using chrome version 79 and have installed chromedriver 79, I tested writing chromedriver in command line and that works which means path is configured right, I have made sure 127.0.0.1 localhost is also in etc/hosts
Below is my code which works on my computer (so i doubt its an issue with the code):
chrome_options = Options()
chrome_options.add_argument("--headless")
with webdriver.Chrome(chrome_options=chrome_options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
In the last question I also tried this modification:
chrome_options = Options()
chrome_options.add_argument("--headless")
with webdriver.Chrome("C:\\chromedriver.exe",chrome_options=chrome_options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before using get()
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
which would instead give me this almost identical error message:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\chromedriver.exe
I am unsure where the issue could lie.
I am using windows by the way.
EDIT: Things I tried and didn't work:
1- running everything as both admin and normal
2- re-installing chrome
3- using the beta-chroma 80 and webdriver 80
4- using normal chrome 79 and webdriver 79
5- Having both the script and the driver in the same directory (while using a
correct path)
6- Having an external path and have it setup as needed
7- Using it in the PATH folder.
8- Adding "127.0.0.1 localhost" to etc/hosts
9- Running service test
I have ensured in every test that everything was in it's correct placement, I have ran a reboot before every new test, and they always give me the same error, which also happens to occur if I had an incorrect path as well, but once I ran the code for a service and it gave me a different error as I had my webdriver in C:/ which required admin privilages, however re-running the test again with the correct privilages gave back the same error
Update the issue isn't exclusive to the chrome driver. Even following setup instructions for either the Firefox or edge drivers end up on the same issues. It makes me suspect that the connection is facing some issue. I have tried running the test codes provided by Mozilla for the setup and it didn't work.
Unsure if that does help much or at all.
This error message...
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
You need to take care of a couple of things:
Ensure that you have downloaded the exact format of the ChromeDriver binary from the download location pertaining to your underlying OS among:
chromedriver_linux64.zip: For Linux OS
chromedriver_mac64.zip: For Mac OSX
chromedriver_win32.zip: For Windows OS
Ensure that /etc/hosts file contains the following entry:
127.0.0.1 localhost
Ensure that ChromeDriver binary have executable permission for the non-root user.
Ensure that you have passed the proper absolute path of ChromeDriver binary through the argument executable_path as follows:
with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=chrome_options) as driver:
So your effective code block will be:
options = Options()
options.add_argument("--headless")
options.add_argument('--no-sandbox') # Bypass OS security model
options.add_argument('--disable-gpu') # applicable to windows os only
options.add_argument("--disable-dev-shm-usage") # overcome limited resource problems
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
with webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options=options) as driver:
driver.set_window_size(800, 460) # takes two arguments, width and height of the browser and it has to be called before
driver.execute_script("document.body.style.zoom='150%'")
driver.get("file:\\"+url) # takes one argument, which is the url of the website you want to open
driver.find_element_by_tag_name('body').screenshot(output) # avoids scrollbar
Mandatory Considerations
Finally, to avoid incompatibility between the version of the binaries you are using ensure that:
Selenium is upgraded to current levels Version 3.141.59.
ChromeDriver is updated to current ChromeDriver v79.0.3945.36 level.
Chrome is updated to current Chrome Version 79.0 level. (as per ChromeDriver v79.0 release notes)
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
Take a System Reboot.
Execute your #Test as non-root user.
References
You can find a couple of reference discussions in:
Python Selenium “Can not connect to the Service %s” % self.path in linux server
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service chromedriver.exe while opening chrome browser
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

Python in Selenium Automation

I am trying to run selenium by running the below two lines of code after giving the path to chromedriver..
from selenium import webdriver
driver=webdriver.Chrome("C:/Users/Admin/Desktop/chromedriver")
But its not working and displaying the below error.
selenium.common.exceptions.WebDriverException: Message: Service C:/Users/Admin/Desktop/chromedriver unexpectedly exited. Status code was: 1
Can you please tell how to fix it ?
Check your chrome brower version and download chromedriver accordingly from below url https://sites.google.com/a/chromium.org/chromedriver/downloads
from selenium import webdriver
browser = webdriver.Chrome(executable_path=r" path of chromedriver.exe")
browser.get('your url')
i think you are missing some libraries, i had a similar problem when i was trying stuff with selenium. try installing the chromium driver as well cause it might have the libraries you need, even if you don't want to use chromium

Webdriver crashes when using it with Selenium. How to resolve it?

I'm following this online resource, for building an Instagram bot. In it, the instructor uses selenium, which I installed by running: pip3 install selenium
Then he mentioned using ChromeDriver. I downloaded it and installed it following this resource. I tested it and seemingly it worked, however when I tried to use it in my script it crashed, an error message occured: Error sending sync broker message: the pipe is being closed (0xE8)
The script which I ran when the error occurred is:
from selenium import webdriver
class InstaBot:
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('https://instagram.com')
InstaBot()
How my I resolve this error? I read through dozens of articles, but still not found what causes. Help is appreciated.
I have checked your code and it works fine for me.
Try to update your chrome browser to the latest version (79.0.3945.130) and download its match ChromeDriver version (79.0.3945.36).
Note that the ChromeDriver should be on the same folder with your code.

Categories