Python: Selenium: Unable to get browser console logs - python

Good morning,
I was trying to access a web application and collect browser console logs (Web Developer -> Web Console).
I am getting an error
File "test1.py", line 28, in <module>
logs = driver.get_log('browser')
File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1262, in get_log
return self.execute(Command.GET_LOG, {'type': log_type})['value']
File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 208, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: HTTP method not allowed
my code is like this.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.support.ui import WebDriverWait
binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options = Options()
options.binary = binary
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True
cap['loggingPrefs'] = {'driver': 'ALL'}
driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path="C:\Python\geckodriver.exe")
driver.get('https://facebook.com')
time.sleep(2) # time for page to load
logs = driver.get_log('browser')
messages = map(lambda l: l['message'], logs)
has_console_logs = any(map(lambda m: m.find('console log') >= 0, messages))
print('Success' if has_console_logs else 'Failure')
driver.quit()

Related

Unable to run selenium on heroku with python script

I'm trying to run a simple python script that open firefox and search some info on https://biblioteca.aneel.gov.br/.
On my computer the script works, but when i try to run on heroku, it always crashes. I tried all tutorials that I could find, but I don't know what more to do.
I did:
I put as 'Buildpacks' on heroku: https://github.com/pyronlaboratory/heroku-integrated-firefox-geckodriver
In 'Config Vars' on heroku:
FIREFOX_BIN = /app/vendor/firefox/firefox
GECKODRIVER_PATH = /app/vendor/geckodriver/geckodriver
LD_LIBRARY_PATH = /usr/local/lib:/usr/lib:/lib:/app/vendor
PATH = /usr/local/bin:/usr/bin:/bin:/app/vendor/
Adjuste code on heroku:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from bs4 import BeautifulSoup
import re
import yagmail
import time
import os
options = webdriver.FirefoxOptions()
# enable trace level for debugging
options.log.level = "trace"
options.add_argument("-remote-debugging-port=9224")
options.add_argument("-headless")
options.add_argument("-disable-gpu")
options.add_argument("-no-sandbox")
binary = FirefoxBinary(os.environ.get('FIREFOX_BIN'))
rea_inicial='teste'
while True:
driver = webdriver.Firefox(firefox_binary=binary, executable_path=os.environ.get('GECKODRIVER_PATH'), options=options)
#driver.maximize_window()
driver.get("https://biblioteca.aneel.gov.br/Busca/Avancada")
print('entrando no site...')
#time.sleep(5)
# Busca por legislacoo
driver.find_element(By.XPATH, '/html/body/main/div/div/div[2]/div/div/button[2]').click()
wait = WebDriverWait(driver, 30)
print('chegou ate aqui')
Then, the error after deploy on heroku:
2021-11-05T17:40:55.035807+00:00 app[worker.1]: code.py:24: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
2021-11-05T17:40:55.035820+00:00 app[worker.1]: driver = webdriver.Firefox(firefox_binary=binary, executable_path=os.environ.get('GECKODRIVER_PATH'), options=options)
2021-11-05T17:40:55.035825+00:00 app[worker.1]: code.py:24: DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
2021-11-05T17:40:55.035825+00:00 app[worker.1]: driver = webdriver.Firefox(firefox_binary=binary, executable_path=os.environ.get('GECKODRIVER_PATH'), options=options)
2021-11-05T17:40:55.540760+00:00 app[worker.1]: Traceback (most recent call last):
2021-11-05T17:40:55.540774+00:00 app[worker.1]: File "code.py", line 24, in <module>
2021-11-05T17:40:55.540947+00:00 app[worker.1]: driver = webdriver.Firefox(firefox_binary=binary, executable_path=os.environ.get('GECKODRIVER_PATH'), options=options)
2021-11-05T17:40:55.540949+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 175, in __init__
2021-11-05T17:40:55.541113+00:00 app[worker.1]: self.service.start()
2021-11-05T17:40:55.541123+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 101, in start
2021-11-05T17:40:55.541249+00:00 app[worker.1]: self.assert_process_still_running()
2021-11-05T17:40:55.541250+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 113, in assert_process_still_running
2021-11-05T17:40:55.541359+00:00 app[worker.1]: raise WebDriverException(
2021-11-05T17:40:55.541409+00:00 app[worker.1]: selenium.common.exceptions.WebDriverException: Message: Service /app/vendor/geckodriver/geckodriver unexpectedly exited. Status code was: 64
Can anyone helps me how to solve it ?
The following arguments:
-no-sandbox
-disable-gpu
perhaps are applicable to ChromeDriver / google-chrome combo but Not Applicable to GeckoDriver / firefox combo.
Remove the arguments and re-execute.

Python Selenium Handle popup alert

So I'm trying to make this program where I make it search for an anime on nyaa.si with filters and such, and i wanted to spice it up a little. Long story short, when I tap on the magnet button, it always asks me to prompt permission for qbittorrent to open the magnet link.PICTURE Here's the code, but the important section is at the end of it and the error I get is this:
Traceback (most recent call last):
File "c:/Users/Asus/Documents/Projects/python/selNyaaSi/main.py", line 49, in <module>
driver.switch_to.alert().accept();
File "c:\users\asus\appdata\local\programs\python\python38\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 55, in alert
alert.text
File "c:\users\asus\appdata\local\programs\python\python38\lib\site-packages\selenium\webdriver\common\alert.py", line 67, in text
return self.driver.execute(Command.W3C_GET_ALERT_TEXT)["value"]
File "c:\users\asus\appdata\local\programs\python\python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "c:\users\asus\appdata\local\programs\python\python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
(Session info: chrome=91.0.4472.124) alert
code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
from time import sleep
from selenium.webdriver.common.alert import Alert
headless = False
path = 'C:\\chromedriver.exe'
options = webdriver.ChromeOptions()
if input('headless?: ') != '':
headless = True
options.add_argument('headless')
with open('urls.json', 'r') as f:
data = json.load(f)
driver = webdriver.Chrome(path, options=options)
url = data['urls']['nyaa']
driver.get(url)
if headless == False:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'themeToggle'))).click()
driver.find_element_by_class_name('navbar-toggle.collapsed').click()
driver.find_elements_by_class_name('form-control')[0].send_keys('Bleach')
temp = driver.find_elements_by_css_selector('select.form-control')
_filter = temp[0]
_category = temp[1]
_filter.click()
driver.find_element_by_css_selector('select.form-control[title=Filter] option[title="Trusted only"]').click()
_category.click()
driver.find_element_by_css_selector('select.form-control[title=Category] option[value="1_2"]').click()
driver.find_element_by_css_selector('button.btn.btn-primary.form-control').click()
temp = driver.find_elements_by_css_selector('tr.success td.text-center i.fa.fa-fw.fa-magnet')
temp[0].click()
driver.switch_to.alert().accept()
I dont see anything about alerts in your code, only the import. You should have something like this after the magnet.click():
driver.switch_to.alert().accept(); # or dismiss() if you dont want to accept

Selenium.common.exceptions.WebDriverException: Message: unknown error: no chrome binary

I'm trying to run a python script I made a few months ago which uses selenium to scrape a web page. Here's my code:
import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="/users/aliallam/Documents/chromedriver")
Here's the full error I'm getting:
Traceback (most recent call last):
File "/Users/aliallam/Desktop/MISOS_Python_Scraper/main.py", line 16, in <module>
driver = webdriver.Chrome(executable_path="/users/aliallam/Documents/chromedriver")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/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: cannot find Chrome binary
I tried the solution under this question, but still no luck: Selenium gives "selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary" on Mac
Here's what I changed my code to:
options = webdriver.ChromeOptions()
options.binary_location = " /Applications/Google\ Chrome\ 2.app"
chrome_driver_binary = "/users/aliallam/Documents/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary,chrome_options=options)
This is really frustrating, some help would be much appreciated! Thanks in advance!
EDIT: In addition to the following mistake, I think you also need to change the binary location to be /Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome
Based on what you posted, I think the error is because of an extra space located after the first quotation mark in this line:
options.binary_location = " /Applications/Google\ Chrome\ 2.app"
Try changing that to:
options.binary_location = "/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome"
and rerunning the code.
Full code based on what you provided:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome"
chrome_driver_binary = "/users/aliallam/Documents/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary,chrome_options=options)
If that doesn't work, you could also try
from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google Chrome 2.app/Contents/MacOS/Google Chrome"
executable_path = "/users/aliallam/Documents/chromedriver"
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=options)
For me on MacOS the error got solved when I moved my downloaded "Google Chrome" application from "Downloads" folder to "Applications" Folder

Selenium Python Error

I am trying to create a small test program that is able to login into gmail. So far the program opens up the website on chrome but then fails to actually type anything into the "Enter Email" form box. Also I am receiving an error within the shell which may provide insight into my question.
Driver Version: 2.40
Chrome Version: 67.0.3396.99
Below is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver= webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')
driver.get("http://mail.google.com")
emailid=driver.find_element_by_name("identifier")
emailid.send_keys("samplekeys")
Below is the error:
Traceback (most recent call last):
File "C:\gmail.py", line 7, in <module>
driver.get("http://mail.google.com")
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 332, in get
self.execute(Command.GET, {'url': url})
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Python36\lib\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: missing or invalid 'entry.level'
(Session info: chrome=67.0.3396.99)
(Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.17134 x86_64)
Any help would be greatly appreciated!
try this :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome(executable_path = r'C:/chromedriver_win32/chromedriver.exe')
browser.get("http://mail.google.com")
wait = WebDriverWait(browser, 10)
user_name = wait.until(EC.element_to_be_clickable((By.ID, 'identifierId')))
user_name.click()
user_name.send_keys("samplekeys")
You need to download the chromedriver, and put the path to your webdriver.Chrome(), for example my chromedriver are in /usr/local/bin/chromdriver
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
or for widndows
driver = webdriver.Chrome(executable_path = r'C:\chromedriver_win32\chromedriver.exe')

Unable to upload image in selenium python

The Code i am using:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
wait = WebDriverWait
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path=r'C:\\chromedriver\\chromedriver.exe')
driver.maximize_window()
driver.get("https://www.arttoframe.com/canvas_acrylic/")
driver.switch_to_frame("Uploadimage")
driver.find_element_by_xpath('//*[ # id = "dropTarget"] / img').send_keys("C:\\Users\\Dell\\Downloads\\340.JPG")
Error:
C:\Users\Dell\PycharmProjects\ATF_TestOrder\venv\Scripts\python.exe
C:/Users/Dell/PycharmProjects/ATF_TestOrder/ATF_TestOrder/ATF_TestOrder.py
Traceback (most recent call last): File
"C:/Users/Dell/PycharmProjects/ATF_TestOrder/ATF_TestOrder/ATF_TestOrder.py",
line 18, in
driver.switch_to_frame("Uploadimage") File "C:\Users\Dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 775, in switch_to_frame
self._switch_to.frame(frame_reference) File "C:\Users\Dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\switch_to.py",
line 89, in frame
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference}) File
"C:\Users\Dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 312, in execute
self.error_handler.check_response(response) File "C:\Users\Dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchFrameException: Message: no such
frame (Session info: chrome=65.0.3325.181) (Driver info:
chromedriver=2.37.544315
(730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT
10.0.16299 x86_64)
I may be wrong, but i think you have to find the iframe first and switch to it
driver.switch_to_frame(driver.find_element_by_id('Uploadimage'))
assuming Uploadimage is an id, you may have to change this one
The website is still valid, so I give it a try, and it is working with below script. The input element for file can't be set in code, so I use send_text to simulate user input.
from clicknium import clicknium as cc
if not cc.chrome.extension.is_installed():
cc.chrome.extension.install_or_update()
browser = cc.chrome.open(url="https://www.arttoframe.com/canvas_acrylic/")
browser.find_element_by_xpath('//*[#id="imageUploadNameText"]').click()
browser.find_element_by_xpath('//*[#id="Upload_content"]/div[1]/div/button').click(by='mouse-emulation')
cc.send_text(r'"C:\Users\Dell\Downloads\340.JPG"')
cc.send_hotkey('{Enter}')
browser.find_element_by_xpath('//*[#id="Edit_content"]/div[3]/ul/li[5]/button').click()

Categories

Resources