I try to open browser using Selenium in Python and after the browser opens, it exits from it, I tried several ways to write my code but every possible way works this way. Thank you in advance for help
`from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get("https://amazon.com")`
I expected the browser to open amazon.com and stay like this until I close or the programme close it. Actual result - when the browser loads the website, it exists from itself.
CodePudding user response:
It looks like you are using the webdriver.Chrome class to create your Chrome driver instance. This class has a service parameter that you can use to specify the Chrome service that should be used to start the Chrome browser.
In your code, you are creating a Chrome service using the Service class and passing it to the webdriver.Chrome class as the service parameter. However, you are not starting the Chrome service before creating the driver instance. To fix this, you can call the start() method on the Chrome service before creating the driver instance, like this:
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
# Create the Chrome service
s = Service(ChromeDriverManager().install())
# Start the Chrome service
s.start()
# Create the driver instance using the Chrome service
driver = webdriver.Chrome(service=s)
# Open the website
driver.get("https://amazon.com")
This should start the Chrome service before creating the driver instance, which should prevent the browser from exiting immediately after opening. You can then use the driver.quit() method to close the browser when you are done.
CodePudding user response:
Use driver.close() function after getting result ;)