0

I am trying to write a program with for loop and for that I need to declare Selenium webdriver before the loop. Each time I am trying to do it I get an error. Like the loop doesn't recognize driver.get part.

Here is the code:

from selenium import webdriver
import time
import csv



with open('smlxl.csv', 'r') as myFile:
    myReader = csv.reader(myFile)
    myList = list(myReader)

driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\chromedriver.exe')

for n in range(1,1000,4):

    barcode = (myList[n][24])
    url = ("https://www.google.com/search?q=" + barcode)
    driver.get(url)
    print(n)

    n = (n+4)
    time.sleep(3)
    driver.close()

The error I get:

C:\Users\DELL\PycharmProjects\pythonProject3\erro.py:10: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\chromedriver.exe')
1
Traceback (most recent call last):
  File "C:\Users\DELL\PycharmProjects\pythonProject3\erro.py", line 16, in <module>
    driver.get(url)
  File "C:\Users\DELL\PycharmProjects\pythonProject3\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 447, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\DELL\PycharmProjects\pythonProject3\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
    self.error_handler.check_response(response)
  File "C:\Users\DELL\PycharmProjects\pythonProject3\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
Stacktrace:
Backtrace:
    Ordinal0 [0x00326463+2188387]
    Ordinal0 [0x002BE461+1762401]
    Ordinal0 [0x001D3C40+801856]
    Ordinal0 [0x001F68BD+944317]
    Ordinal0 [0x001F7AD6+948950]
    GetHandleVerifier [0x005C71F2+2712546]
    GetHandleVerifier [0x005B886D+2652765]
    GetHandleVerifier [0x003B002A+520730]
    GetHandleVerifier [0x003AEE06+516086]
    Ordinal0 [0x002C468B+1787531]
    Ordinal0 [0x002C8E88+1805960]
    Ordinal0 [0x002C8F75+1806197]
    Ordinal0 [0x002D1DF1+1842673]
    BaseThreadInitThunk [0x7587FA29+25]
    RtlGetAppContainerNamedObjectPath [0x770D7A9E+286]
    RtlGetAppContainerNamedObjectPath [0x770D7A6E+238]


Process finished with exit code 1

Thank you for helping

unifari56
  • 11
  • 1
  • What are the expected values in `barcode = (myList[n][24])`? – undetected Selenium Jul 22 '22 at 09:32
  • it's numbers. The first search works and it fails in the second time. If I change the location of "driver = webdriver.Chrome(executable_path='C:\Program Files (x86)\chromedriver.exe')" to inside the loop it works fine but this is not what I am looking for. – unifari56 Jul 22 '22 at 09:35

2 Answers2

1

Had to place driver.close() outside the for loop.

unifari56
  • 11
  • 1
-1

These error message...

selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id

...implies that the previous session wasn't successfully killed/removed/eliminated.


Solution

You need to replace:

driver.close()

with:

driver.quit()

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you! the replacement didn't helped but replacing driver.close() outside the for loop fixed it. – unifari56 Jul 22 '22 at 09:59
  • In your question code `driver = webdriver.Chrome()` is out of `if` scope, why would you keep `driver.close()` within `if` scope? Those are python basics. I thought indentation issue while code pasting. Anyways, I should have pointed it out. Good luck. – undetected Selenium Jul 22 '22 at 11:11