(Feb-13-2019, 02:08 PM)gahhon Wrote: So my concern is how to check if chrome running/exist? If so then how to launch new tab?you only ever need to do this ever once in your program
driver = webdriver.Chrome(executable_path=r'chromedriver.exe')Whereas driver.get(URL) you have to do numerous times to open each tab. Ive already explained how to switch tabs, and how to open a new tab earlier. driver.get(URL) will open the first tab if chrome is not open. If chrome is already open with a tab, then it will load the url on the first tab again with driver.get(url) regardless. To make a new tab you just switch windows before driver.get(url). The previous tab will remain until you close it and you will load and switch focus to the new tab. So there is no need for a check if chrome is running as driver.get(url) will open the first tab or a new tab depending on which window you are on.
As a side note. chromedriver will open a separate process than your chrome browser you are using. It is not meant to piggyback on your browser but open a new one being handled by selenium. chromedriver will have no relation to chrome on your computer.
# Opens a new tab driver.execute_script("window.open()") # Switch to the newly opened tab driver.switch_to.window(driver.window_handles[1]) # Navigate to new URL in new tab driver.get("https://google.com") # Run other commands in the new tab here You're then able to close the original tab as follows # Switch to original tab driver.switch_to.window(driver.window_handles[0]) # Close original tab driver.close() # Switch back to newly opened tab, which is now in position 0 driver.switch_to.window(driver.window_handles[0]) Or close the newly opened tab # Close current tab driver.close() # Switch back to original tab driver.switch_to.window(driver.window_handles[0])
Recommended Tutorials: