I tried to make a bot which looks up if the Bob Marley TShirt from Ajax Amsterdamn is available.
https://www.adidas.ch/de/ajax-3-jsy/GT9559.html <- there is the link
I was able to get it running (also added an telegram bot to report the succses:
#!/usr/bin/python3
from selenium import webdriver
import requests
token="" # expunged for obvious reasons
chat="" # expunged for obvious reasons
message="Available"
fireFoxOptions = webdriver.FirefoxOptions()
fireFoxOptions.set_headless()
browser = webdriver.Firefox(firefox_options=fireFoxOptions)
browser.get("https://www.adidas.ch/de/ajax-3-jsy/GT9559.html")
if ("Dieses Produkt ist leider ausverkauft." not in browser.page_source):
send_text = 'https://api.telegram.org/bot' token '/sendMessage?chat_id=' chat '&parse_mode=Markdown&text=' message
response = requests.get(send_text)
print(response.json())
browser.close()
Working on:
- selenium 3.141.0
- python 3
- Firefox 91.0.2
- geckodriver 0.29.1
- OS: Manjaro Linux
So after that I tried to deploy it on my Debian 10 Server but here is where the struggle began. I had to install Firefox 78.14.0esr and according to the github release page of the Geckodriver version 0.27.0 of it. Selenium stayed the same with 3.141.0. From what I know and what I researched the versions should be alright but when executed throw this nervewrecking error:
Traceback (most recent call last):
File "./ajax.py", line 18, in <module>
browser = webdriver.Firefox(options=options) #, capabilities=cap, executable_path="/usr/local/bin/geckodriver")
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/webadmin/.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.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
I searched the error up and apparently you have to define the binary paths and the capability "marionette" now I did this and now the code looks like this (including some debugging stuff):
#!/usr/bin/python3
from selenium import webdriver
import requests
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#token="1995953311:AAH-D7S-MISkCa0yQxUc84Gf978fz0vtoqY"
#chat="1917512203"
message="just a test"
options = FirefoxOptions()
options.add_argument("--headless")
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
binary = "/usr/bin/firefox"
options.binary = binary
browser = webdriver.Firefox(options=options, capabilities=cap, executable_path="/usr/local/bin/geckodriver")
browser.get("https://www.adidas.ch/de/ajax-3-jsy/GT9559.html")
if ("Dieses Produkt ist leider ausverkauft." not in browser.page_source):
send_text = 'https://api.telegram.org/bot' token '/sendMessage?chat_id=' chat '&parse_mode=Markdown&text=' message
response = requests.get(send_text)
print(response.json())
else:
print("succ")
browser.close()
But now I get the following error:
Traceback (most recent call last):
File "./ajax.py", line 18, in <module>
browser = webdriver.Firefox(options=options, capabilities=cap, executable_path="/usr/local/bin/geckodriver")
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 191, in __init__
self.binary, timeout)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 73, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "/home/webadmin/.local/lib/python3.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 104, in _wait_until_connectable
"The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
Also changing the cap["marionette"] = False
to True
just gives me the older error message.
Thank You!
CodePudding user response:
I just reverted all the changes, made an Docker container and put that on the server.