0

I am a beginner in programming, and in selenium as well. Therefore, I am seeking for your help in fixing the code to set os.environ in selenium4 as below.

  service = ChromeService(executable_path="D:\\port\\driver\\chromedriver")
  os.environ["wedriver.chrome.driver"] = service  
  driver = webdriver.Chrome(service=service) 
  driver.get(https://gisgeography.com/free-satellite-imagery-data-list/')
  driver.maximize_window()
James Z
  • 12,209
  • 10
  • 24
  • 44
Vinh
  • 13
  • 2
  • question is unclear. if you are trying to set an environment variable you have a typo in wedriver. If you are trying to just set the web driver refer to this https://stackoverflow.com/questions/42478591/python-selenium-chrome-webdriver – Vijayaraghavan Sundararaman May 01 '22 at 06:11
  • Dear Sir, Thank you so much for your prompt feedback. Yes. Please help me in setting an environment variable if possible as I can't find my own typos mistake. I very much look forward to hearing more from you. Best regards, – Vinh May 02 '22 at 04:14

1 Answers1

0

You will not need to use an environment variable for this.

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

s=Service('D:\\port\\driver\\chromedriver')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)

however if you need to set ot read environment variables using python below is an example

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# Setting an environment variable
os.environ["CHROME_DRIVER_PATH"] = "D:\\port\\driver\\chromedriver"
# Reading an environment variable
DRIVER_PATH = os.environ.get('CHROME_DRIVER_PATH')
s=Service(DRIVER_PATH)
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)
  • Thank you so much Sir! It is indeed very helpful. By the way, I would be very grateful if you could enlighten me a bit more on why I don't need to use an environment variable for this please! – Vinh May 03 '22 at 05:06
  • You can use environment variables if you plan to use this in multiple projects and want to reference with a name like `CHROME_DRIVER_PATH ` without having to remember the actual path. Or you want to run the same project in different environments and do not want to change the code based on environment so you set the environment variable and reference it with the name. – Vijayaraghavan Sundararaman May 06 '22 at 05:26
  • Dear Vijayaraghavan, Thank you so much for your clear explanations. By the way, I have tried with your help in setting the environment variables. It works so well. I am very grateful to your help. I am wondering whether you will generously allow me to contact you by email in future please. If yes, I would very much appreciate of you will give me you contact info please! Thank you again and have a good weekend. With very best regards, Vinh – Vinh May 08 '22 at 03:35
  • @Vinh As you have found the answer provided resolves you're question, ensure that you mark it as the accepted answer and upvote it, so the community is aware and can acknowledge it for future searches – djmonki May 10 '22 at 03:23