Home > Software design >  I tried to open the site with this code using selenium, but the site was closed as soon as it was op
I tried to open the site with this code using selenium, but the site was closed as soon as it was op

Time:01-04

When I run this code, the page opens and closes. I can't reach the page. Everything is in the latest version. I am a Windows user.

I was trying to open Instagram page with this code.

enter image description here

I tried to open the instagram site with this code and the site was closed as soon as it was opened.

CodePudding user response:

Im not familiar with selenium but I know for a fact that get() just gets the HTML code of the website and does not display it

CodePudding user response:

You have to add the below Chrome Option:

options.add_experimental_option("detach", True)

Full code:

from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(chrom_driver_pat), options=options)
driver.get(<url>)

Next time, don't post the image, post the code and explain your issue clearly.

CodePudding user response:

First of all, please, do not use images when your want a code review or help, it is more easier to help with raw code.

Selenium now uses Service method to run webdriver.

To run what you want, you need to fix some parts of your code, like this:

from selenium import webdriver 
# in my case, i'm using selenium==4.5.0, for this version, is necessary to
# use selenium.webdriver.chrome.service to use driver path
from selenium.webdriver.chrome.service import Service as ChromeService

# For Windows path, use r"C:\..." when you will use \ 
# Remember to set the executable file too
chrome_driver_path = r"C:\pythondriver\chromedriver\chromedriver.exe"
# you can use "universal" path too, like linux, using / instead of \
# chrome_driver_path = "C:/pythondriver/chromedriver/chromedriver.exe"

# instead using: 

# webdriver.Chrome()
# driver = webdriver.Chrome(chrome_driver_path)

# use this:
chrome_service = ChromeService(chrome_driver_path)

driver = webdriver.Chrome(service=chrome_service)

url = "https://instagram.com"

driver.get(url)

I tested using the configurations below and worked for me:

Chromedriver - 108.0.5359.71
Chrome - 108.0.5359.125 - 64 bits

If you need more help using Chrome Service, try look the Selenium Docs

  • Related