Python Forum
webdriver.remote to connect back existing browser without open new browser?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
webdriver.remote to connect back existing browser without open new browser?
#1
CommonChrome.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ChromeDriver:

    def __init__(self, remote = False):
        driverOtp = webdriver.ChromeOptions()
        driverOtp.add_argument("--start-maximized")
        driverOtp.add_argument("--disable-infobars")
        driverOtp.add_argument("--ignore-certificate-errors")
        driverOtp.add_argument("--disable-extensions")
        driverOtp.add_argument("--disable-gpu")
        driverOtp.add_argument("--log-level=3")
        
        if remote is False:
            self.driver = webdriver.Chrome(options=driverOtp, executable_path=r'chromedriver.exe')
            ch.write("url", self.driver.command_executor._url)
            ch.write("session_id", self.driver.session_id)
            self.switchTab(1)
            self.close()
            self.switchTab(0)
        else:
            sID = ch.get("session_id")
            sURL = ch.get("url")
            
            self.driver = webdriver.Remote(command_executor=sURL, desired_capabilities={})
            self.driver.close()
            self.driver.quit()
            self.driver.session_id = sID
            self.newTab()
            self.switchLastTab()
            
        self.driver.implicitly_wait(30)        
        self.wait = WebDriverWait(self.driver, 30)
Google.py
import CommonChrome as Chrome
import ChromeIni as CI

def google():
    ch = CI.Ini()
    
    sID = ch.get("session_id")
    sURL = ch.get("url")

    if sID is "" or sURL is "":
        driver = Chrome.ChromeDriver()
    else:
        driver = Chrome.ChromeDriver(True)

    driver.redirect("https://www.google.com")
Yahoo.py
import CommonChrome as Chrome
import ChromeIni as CI

def yahoo():
    ch = CI.Ini()
    
    sID = ch.get("session_id")
    sURL = ch.get("url")

    if sID is "" or sURL is "":
        driver = Chrome.ChromeDriver()
    else:
        driver = Chrome.ChromeDriver(True)

    driver.redirect("https://www.yahoo.com")
Execution Scenario: Execute google() then execute yahoo()

Current outcome:
  1. New chrome browser is launched and redirect to www.google.com
  2. New chrome browser is launched, and closed, then attached new tab in 1. browser
  3. New tab is redirect to www.yahoo.com

Expected outcome:
  1. New chrome browser is launched and redirect to www.google.com
  2. New chrome browser is launched and hidden the window, and closed, then attached new tab in 1. browser
  3. New tab is redirect to www.yahoo.com

So how can I launch new browser in hidden mode?
Thanks
Reply
#2
Anyone? hmmm
Reply
#3
(Feb-23-2019, 05:57 AM)gahhon Wrote: So how can I launch new browser in hidden mode?
add this argument
driverOtp.add_argument("--headless")
Recommended Tutorials:
Reply
#4
(Feb-24-2019, 01:26 PM)metulburr Wrote:
(Feb-23-2019, 05:57 AM)gahhon Wrote: So how can I launch new browser in hidden mode?
add this argument
driverOtp.add_argument("--headless")

I did try that, but it didn't work as well.
"--headless" or "--no-startup-window"

My method of trying is,
driverOtp.add_argument("--headless")
then convert them to capabilities and assign to the webdriver.remote
but still not working.
Reply
#5
(Feb-24-2019, 06:58 PM)gahhon Wrote: I did try that, but it didn't work as well.
in what way did it not work?
Recommended Tutorials:
Reply
#6
I follow this guideline here Set ChromeOption with RemoteDriver
So my code would like like:
sID = ch.get("session_id")
sURL = ch.get("url")

options = webdriver.ChromeOptions()
## options.add_argument('--headless') or options.add_argument('--no-startup-window')

self.driver = webdriver.Remote(command_executor=sURL, desired_capabilities=options.to_capabilities())
self.driver.close()
self.driver.quit()
self.driver.session_id = sID
self.newTab()
self.switchLastTab()
In both way, they still will launch a new browser window.
Reply
#7
I have resolved the issue, but I am not sure whether is it the quotation issue or not.

driverOtp = webdriver.ChromeOptions()
driverOtp.add_argument('--headless')
driverOtp.add_argument('--disable-gpu')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  m3u8 using build-in browser downloader? kucingkembar 4 935 Jun-13-2024, 12:39 PM
Last Post: NolaCuriel
  Unable to convert browser generated xml to parse in BeautifulSoup Nik1811 0 512 Mar-22-2024, 01:37 PM
Last Post: Nik1811
  Creating a Browser Extension using Python j49857 3 1,551 Feb-13-2024, 10:49 PM
Last Post: j49857
  Need help for script access via webdriver to an open web page in Firefox Clixmaster 1 1,412 Apr-20-2023, 05:27 PM
Last Post: farshid
  [Solved] Browser won't parse text from yaml loaded into Jinja SpongeB0B 1 1,083 Jul-07-2022, 09:37 PM
Last Post: SpongeB0B
  Problem with Selenium webdriver Fred 1 2,161 Jan-10-2022, 05:45 PM
Last Post: Larz60+
  Connect to existing Firefox session with Selenium euras 0 5,657 Feb-11-2021, 02:54 PM
Last Post: euras
  Which webdriver is required for selenium in Pydroid App Rahatt 1 6,538 Jul-31-2020, 01:39 AM
Last Post: Larz60+
  Log In Button Won't Click - Python Selenium Webdriver samlee916 2 3,982 Jun-07-2020, 04:42 PM
Last Post: samlee916
  Record audio from the browser uppie83 1 4,404 Jun-04-2020, 11:20 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020