SwagZ
SwagZ

Reputation: 817

Selenium Remote Webdriver not working with AWS EC2

I'm trying to do some website testing which requires to keep the old webdriver open then use webdriver.remote to re-attach back using the executor url and session id of the old driver. Same code runs fine on my MacBook, but running into error on AWS EC2 Ubuntu 16.04. Error Trace back and code are attached below. Please help.

OS: Ubuntu 16.04

Selenium Version: 3.4.0

Browser: Google-Chrome

enter image description hereenter image description here

New Code following @Tarun Lalwani's Idea

Upvotes: 0

Views: 3024

Answers (1)

SwagZ
SwagZ

Reputation: 817

After digging so long for this issue, I finally found the solution myself. Turns out Ubuntu without GUI is pain in the butt to deal with. So when you try to launch Selenium Webdriver. You need to add a few options not only the regular webdriver.Chrome, but also webdriver.Remote.

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import 
DesiredCapabilities

options = webdriver.ChromeOptions()
options.binary_location = '/usr/bin/google-chrome'
options.add_argument('headless')
options.add_argument('--no-sandbox')

driver = webdriver.Chrome(chrome_options=options)
executor_url = driver.command_executor._url
session_id = driver.session_id
driver.get("http://www.google.com")

print(session_id)
print(executor_url)

print(driver.current_url)

driver2 = webdriver.Remote(command_executor=executor_url, desired_capabilities=options.to_capabilities())
driver2.close()
driver2.session_id = session_id
print(driver2.current_url)
driver2.get("http://www.facebook.com")
print(driver2.current_url)

Also, having webdriver.remote will open up a zombie webdriver too. If you only care about re attach back to existing webdriver. You can close the new driver before the new driver attached back to the old one.

Upvotes: 2

Related Questions