So I've been having an issue with Selenium - it essentially freezes before returning a function.
ids = [] # some set of ids
pData = {}
def getName(id:str) -> str:
# setup selenium options
options = Options()
# setup selenium driver
s = Service('C:\\WebDriver\\chromedriver103.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://someurl/page?id=' id)
try:
element = 1# get some element
print(element.text)
except Exception as e:
print(e)
finally:
driver.close()
return "abcd"
for i in ids:
pName = ids(i)
print("Found name: " pName)
pData[i] = pName
Weirdly, if I retry after a while, it happens instantly, then starts freezing again. I feel like this may be with the service getting locked? But again I'm not too sure why this is happening.
So if I try once after a long time, it instantly prints "abcd". But then if I have mutliple ids in the ids list, it it will print the first one, then get stuck. But if start the entire script again, it will not even show "abcd" the first time as it's already frozen before getting there.
Also, the return value is not important - it's just the fact that it's freezing over and over again.
Disclaimer: I edited out the URL for privacy purposes
Really stumped on this one. Any help would be appreciated. Thank you!
CodePudding user response:
I haven't worked with selenium in ages but try adding a few "sleep" tokens in your code. It could be because your loop is executing too fast for your automation to keep up (Assuming your function is called in a loop from the arrays).
This could potentially solve the issue, try increasing and decreasing the sleep value and see if it helps.
CodePudding user response:
I finally found the answer - it seemed that many people had been experiencing the very same issue, and it's to do with ChromeDriver not exiting fully, even when driver.quit() is called.
import psutil
p = psutil.Process(driver.service.process.pid)
Then when when I want to finish, I call:
driver.close()
p.terminate()
This causes an HTTP connection issue, but I catch that.
This may be a non-orthodox solution, but I'm open to more suggestions even though this answer will be marked as solved.
Thanks for your help!