Working at a company so could be too do with firewall permissions ect, but I am able to modify objects in the same location as the .exe. I have the code below. The chrome session opens and maximises but does not go to the webpage or enter any elements.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Users\ME\Local\Google\Chrome\Application\chrome.exe')
driver.maximize_window()
driver.get("http://www.seleniumeasy.com/test/basic-first-form-demo.html")
assert "Selenium Easy Demo - Simple Form to Automate using Selenium" in driver.title
eleUserMessage = driver.find_element_by_id("user-message")
eleUserMessage.clear()
eleUserMessage.send_keys("Test Python")
eleShowMsgBtn=driver.find_element_by_css_selector('#get-input > .btn')
eleShowMsgBtn.click()
eleYourMsg=driver.find_element_by_id("display")
assert "Test Python" in eleYourMsg.text
driver.close()
But keeps exiting with the error:
C:\Users\ME\MyNewEnv\Scripts\python.exe "F:/Sam/TASKS/xings.py"
Traceback (most recent call last):
File "F:/Sam/TASKS/xings.py", line 3, in <module>
driver = webdriver.Chrome(executable_path=r'C:\Users\ME\Local\Google\Chrome\Application\chrome.exe')
File "C:\Users\ME\MyNewEnv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\ME\MyNewEnv\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start
self.assert_process_still_running()
File "C:\Users\ME\MyNewEnv\lib\site-packages\selenium\webdriver\common\service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service C:\Users\ME\Local\Google\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0
Process finished with exit code 1
You should use chromedriver in executable_path instead of chrome.
You can download chromedriver from https://chromedriver.chromium.org/downloads
After downloading you can do
driver = webdriver.Chrome(executable_path=r'path/to/chromedriver')
Related
I'm writing some code using Selenium, and at one point I make 7 requests, all to different websites. For the first one, this works fine. However, for others, I get a session ID error. I think that my browser is configured correctly, as I do get results from the first website. I have tried to put a WebDriverWait in between the requests, but to no avail. I think the websites might be blocking my requests. Does anyone have any idea how to solve this problem?
I'm sorry if this is something stupid or if I'm doing anything wrong, I'm quite new ^^
Thanks in advance!
Traceback (most recent call last):
File "/home/cena/PycharmProjects/Frikandelbroodje/main.py", line 56, in <module>
dirk_price = get_price(dirk_url, dirk_classname)
File "/home/cena/PycharmProjects/Frikandelbroodje/main.py", line 44, in get_price
browser.get(url)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid session id
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Linux 4.15.0-50-generic x86_64)
invalid session id
The invalid session ID error is a WebDriver error that occurs when the server does not recognize the unique session identifier. This happens if the session has been deleted or if the session ID is invalid.
A WebDriver session can be deleted through either of the following ways:
Explicit session deletion: A WebDriver session is explicitly deleted when explicitly invoking the quit() method as follows:
Code Block:
from selenium import webdriver
from selenium.common.exceptions import InvalidSessionIdException
driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
print("Current session is {}".format(driver.session_id))
driver.quit()
try:
driver.get("https://www.google.com/")
except Exception as e:
print(e.message)
Console Output:
Current session is a9272550-c4e5-450f-883d-553d337eed48
No active session with ID a9272550-c4e5-450f-883d-553d337eed48
Implicit session deletion: A WebDriver session is implicitly deleted when you close the last window or tab invoking close() method as follows:
Code Block:
driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
print("Current session is {}".format(driver.session_id))
# closes current window/tab
driver.close()
try:
driver.get("https://www.google.com/")
except Exception as e:
print(e.message)
Console Output:
Current session is a9272550-c4e5-450f-883d-553d337eed48
No active session with ID a9272550-c4e5-450f-883d-553d337eed48
Conclusion
As the first one request works fine but for others you get a session ID error most possibly the WebDriver controled Web Browser is getting detected and hence blocking the next requests.
There are different reasons for the WebDriver controled Web Browser to get detected and simultaneously get blocked. You can find a couple of detailed discussion in:
How does recaptcha 3 know I'm using selenium/chromedriver?
Selenium and non-headless browser keeps asking for Captcha
I got this error message because I was running Selenium in docker and I hadn't mounted enough swap memory, so it would crash after just a few pages.
To fix this, I used the same docker command, but added -v /dev/shm:/dev/shm after docker run.
If you had this
docker run -d -p 5901:5900 -p 127.0.0.1:4445:4444 selenium/standalone-chrome
then change to this
docker run -v /dev/shm:/dev/shm -d -p 5901:5900 -p 127.0.0.1:4445:4444 selenium/standalone-chrome
I found this info here, and here.
Browser page crash may leads to InvalidSessionIdException. Selenium says to us: session deleted because of page crash. Check if your browser page still exists when you got your errors.
Here an example of a traceback of this case:
[2021-06-28 15:05:43,787: ERROR/ForkPoolWorker-2] Message: invalid session id
Traceback (most recent call last):
...
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: session deleted because of page crash
from tab crashed
(Session info: chrome=83.0.4103.61)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
...
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 580, in find_elements_by_class_name
return self.find_elements(by=By.CLASS_NAME, value=name)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 1007, in find_elements
'value': value})['value'] or []
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id
If you want some technical details, take a look at Chromium sources where you can find string session deleted because of page crash.
I had this problem and the reason was that I wrote url in wrong format - not like this, which is correct:
self.driver.get('https://twitter.com')
But this way:
self.driver.get('twitter.com')
Maybe you had the same issue. If not, just check all the links and make sure that all of them are in format of the correct one
in mycase the issue I executed driver.close() then tried to access current_url property of driver which is already closed
this what my wrong code leads to this error message:
url = 'http://localhost:5000/traning'
webpage = driver.get(url)
time.sleep(2)
driver.close()
return driver.current_url
this returns error:
selenium.common.exceptions.InvalidSessionIdException: Message: invalid
session id
and this is solution just save current url and all data in variable before close driver to return this data
driver = setDriver()
url = 'http://localhost:5000/traning'
webpage = driver.get(url)
current_url = driver.current_url
time.sleep(2)
driver.close()
return current_url
Try this:
driver.get('url')
#some code
current_page = driver.current_url
driver.close()
driver.quit()
time.sleep(10)
driver.get(current_page)
After I put driver.close with driver.quit it solve same issue, as you have
After closing the driver you have to reinitialize it in order to make it start again.
chrome_driver_path = "D:\chromedriver.exe"
s = Service(chrome_driver_path)
driver = webdriver.Chrome(service=s)
driver.get("https://google.com/")
driver.close()
driver = webdriver.Chrome(service=s)
driver.get("https://stackoverflow.com/")
I am trying the following script and can't get it to open Webdriver:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/usr/bin/google-chrome')
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
This produces the following error message:
Traceback (most recent call last):
File "scraper.py", line 4, in <module>
driver = webdriver.Chrome('/usr/bin/google-chrome')
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /usr/bin/google-chrome unexpectedly exited. Status code was: 1
I'm using Ubuntu 16.04 running on Windows 10. Any ideas what this could be?
EDIT:
Now I'm doing this with chromedriver, which I unzipped to the same directory as the script.
driver = webdriver.Chrome(executable_path='./chromedriver')
I get the following error instead of the previous one:
Traceback (most recent call last):
File "scraper.py", line 4, in <module>
driver = webdriver.Chrome(executable_path='./chromedriver')
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/home/joseph/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.36.540471 (9c759b81a907e70363c6312294d30b6ccccc2752),platform=Linux 4.4.0-43-Microsoft x86_64)
While working with Selenium v3.11.0, ChromeDriver v2.36 and Chrome v64.x you have to download the latest ChromeDriver from the ChromeDriver - WebDriver for Chrome and place it within your system. Next while initializing the WebDriver and the WebBrowser you have to mention the absolute path of the ChromeDriver as follows :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.quit()
Note : At the end of your Test Execution instead of close() invoke the quit() method so the WebDriver and WebBrowser both are destroyed.
You may need to try various versions of the driver depending on version of chrome. Also, make sure your path is correct and pointing to executable driver file(.exe)
driver = webdriver.Chrome(executable_path='./chromedriver/chromedriver.exe')
I've just loaded Python Selenium into my Ubuntu system and I'm following the Getting Started tutorial on ReadTheDocs. When I run this program:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
But I'm getting this error:
Traceback (most recent call last):
File "/home/henry/Documents/Scraper/test-selenium.py", line 4, in <module> driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/distpackages/selenium/webdriver/firefox/webdriver.py", line 80, in __init__self.binary, timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__self.binary.launch_browser(self.profile, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser self._wait_until_connectable(timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 108, in _wait_until_connectable % (self.profile.path))
WebDriverException: Message: Can't load the profile. Profile Dir: /tmp/tmp8xr2V3 If you specified a log_file in the FirefoxBinary constructor, check it for details.
I think you are using selenium 2.53.6
The issue is compatibility of firefox with selenium, since firefox>= 48 need Gecko Driver(https://github.com/mozilla/geckodriver) to run testcases on firefox. or you can downgrade the firefox to 46..from this link https://ftp.mozilla.org/pub/firefox/releases/46.0.1/
Looks like there's a problem with the webdriver - do you have Firefox installed on your RasPi?
(If not, this might help: https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=150438)
Why does this happen using selenium with python and firefox? Selenium cant scale to a simple loop. Why cant firefox scale? Over time it decides to thow an error and quit.
Mozilla Firefox 45.0
selenium.version
'2.53.2'
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
mylist = ['page1','page1',...,'pageN']
while True:
for data in mylist:
browser = webdriver.Firefox()
try:
myfunc(data) # Code that uses selenum to fetch pages
except:
pass
browser.quit()
time.sleep(60)
No handlers could be found for logger "sentry.errors.serializer"
Traceback (most recent call last):
File "platrieveerp.py", line 231, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium-2.53.2-py2.7.egg/selenium/webdriver/firefox/webdriver.py", line 103, in __init__
self.binary, timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium-2.53.2-py2.7.egg/selenium/webdriver/firefox/extension_connection.py", line 51, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium-2.53.2-py2.7.egg/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium-2.53.2-py2.7.egg/selenium/webdriver/firefox/firefox_binary.py", line 106, in _wait_until_connectable
% (self.profile.path))
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: /tmp/tmpOgsKOx If you specified a log_file in the FirefoxBinary constructor, check it for details.
Selenium is not failing while running the loop, it is failing at webdriver.Firefox(). The error is recognized when the connection times out at a certain time after line 231 when the script still cant recognize an open firefox application.
It is just a bug in selenium when running with a headless browser. To get a working version of selenium, run
sudo pip install -U selenium
This should be a similar issue:
Selenium: FirefoxProfile exception Can't load the profile
I'm able to run my Python code(main.py) perfectly alright.The problem happens when I use Jenkins to run my Python code( main.py). Jenkins is not able to launch Firefox.
The line
driver = webdriver.Firefox() is throwing an exception line this:
Entering main()
Traceback (most recent call last):
File "main.py", line 21, in <module>
driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
self.binary, timeout),
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
self.binary.launch_browser(self.profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 61, in launch_browser
self._wait_until_connectable()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 100, in _wait_until_connectable
self._get_firefox_output())
selenium.common.exceptions.WebDriverException: Message: "The browser appears to have exited before we could connect. The output was: \n(process:9287): GLib-CRITICAL **: g_slice_set_config: assertion `sys_page_size == 0' failed\nError: no display specified\n"
Finished: SUCCESS
############# Code snippet ###########
if __name__ == __main__ :
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get("www.google.com")
Are you on windows platform and running Jenkins as a windows service?
If so then the remedy will be to run the Jenkins from the command line.