Selenium works only when I'm connected to a remote server
Question:
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
Answers:
The problem was solved by changing the chrome driver initialization as follows:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
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
The problem was solved by changing the chrome driver initialization as follows:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)