I am trying to run a simple Selenium chrome-driver test on a remote Ubuntu server (EC2).
In order to run the code immediately after rebooting, I define the following crontab entry:
@reboot export DISPLAY=:0 && export PATH=$PATH:/usr/local/bin && nohup /usr/bin/python3 /home/ubuntu/test/play/client.py 60 http://192.162.2.2:8080 &
And the client.py
code as follows:
import sys
from seleniumwire import webdriver
from selenium.webdriver.common.by import By
from pyvirtualdisplay import Display
def play(test_duration, address):
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--incognito")
display = Display(visible=False)
display.start()
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=chrome_options)
driver.get(address)
play_btn = driver.find_element(By.XPATH, '/html/body/app-root/div/div[1]/button[1]') # play button
send_stats_btn = driver.find_element(By.XPATH, '/html/body/app-root/div/div[1]/button[2]') # get statistics
play_btn.click()
time.sleep(test_duration)
send_stats_btn.click()
driver.close()
driver.quit()
display.stop()
if __name__ == '__main__':
test_duration = int(sys.argv[1])
address = sys.argv[2]
play(test_duration, address)
The strange thing is that the script worked perfectly when I connected to the machine (rebooting and then logging back in). However, when I logout, I receive the following error:
Message: Service /usr/bin/chromedriver unexpectedly exited. Status code was: 1
2
Answers
The problem was solved by changing the chrome driver initialization as follows:
I have exact same problem. For solving this problem together, I share my environment.
Platform : Oracle Cloud VM
OS : Ubuntu 22.04, arm64
chromium version : 112.0.5615.49
chromedriver version : 112.0.5615.49 (same with chromium)
selenium : 3.14, 4.9 both tested
I tried almost everything what I can find, but my code works well only when I connected to server by SSH.
I run my python script on crontab, it occurs same error code 1.
I suggest that chromedriver use ssh connection as a medium, since whenever I logout my ssh session, my python script occurs connection error at that time.
Hope somebody’s help. thanks.