Selenium can't open Firefox 48.0.1 - python

I am starting to learn how to become a better test driven developer when creating web applications in Django. I am trying to use Selenium to open a browser, but I am getting an error.
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: /var/folders/xn/bvyw0fm97j1_flsyggj0xn9r0000gp/T/tmptoxt890d If you specified a log_file in the FirefoxBinary constructor, check it for details.
I read that by "Installing the FF extension "Disable Add-on Compatibility Checks" skips this and everything is fine." selenium.common.exceptions.WebDriverException: Message: Can't load the profile. I did this, but It is still not working. I used Python2.7 and Python3.5 with Selenium version 2.53.6.
Python file
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import unittest
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox(capabilities=caps)
def tearDown(self):
self.browser.quit()
def test_can_start_a_list_and_retrieve_it_later(self):
self.browser.get('http://localhost:8000')
self.assertIn('To-Do', self.browser.title)
if __name__ == '__main__':
unittest.main(warnings='ignore')
Stack Trace
Creating test database for alias 'default'...
EException ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x103f652b0>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 151, in __del__
self.stop()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 123, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
======================================================================
ERROR: test_can_start_a_list_and_retrieve_it_later (functional_tests.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/timothybaney/Treehouse/TDD/superlists/functional_tests.py", line 13, in setUp
self.browser = webdriver.Firefox(capabilities=caps)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 82, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 62, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
NotADirectoryError: [Errno 20] Not a directory
----------------------------------------------------------------------
Ran 1 test in 0.012s
FAILED (errors=1)
Destroying test database for alias 'default'...

That error is because you are using FF 48. For FF>=47 FirefoxDriver stop working. You must use the new MarionetteDriver
Set up this:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
browser = webdriver.Firefox(capabilities=caps)
browser.get('http://localhost:8000')
assert 'Django' in browser.title

Related

how to connect android virtual device by python appium

when i use real device, this code works successfully
but use android virtual device, this code doesn't work with errors
i tried PC reboot and made a new android virtual device
but not work too
anybody help me T.T
PS. android virtual device successfully work, but app immediately exit after start
this is my code:
import unittest
import os
from appium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class TableSearchTest(unittest.TestCase):
def setUp(self):
app = os.path.join(os.path.dirname(__file__), 'E:\\PycharmProjects\\pythonProject\\workspace\\Test_Calculator', 'ApiDemos-debug.apk')
app = os.path.abspath(app)
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities = {
'appium:app': app,
'appium:platformName': 'Android',
'appium:platformVersion': '11',
'appium:deviceName': 'Pixel_3a_XL_API_30',
# 'appium:deviceName': 'R3CN208XN9R',
'appium:automationName': 'Appium',
'appium:appPackage': 'io.appium.android.apis',
'appium:appActivity': 'io.appium.android.apis.ApiDemos',
})
def test_search_field(self):
driver = self.driver
wait = WebDriverWait(driver, 20)
sleep(30)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TableSearchTest)
unittest.TextTestRunner(verbosity=2).run(suite)
this is error message:
C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\unittest\case.py:618: ResourceWarning: unclosed <socket.socket fd=560, family=2, type=1, proto=0, laddr=('127.0.0.1', 54143), raddr=('127.0.0.1', 4723)>
with outcome.testPartExecutor(self):
ResourceWarning: Enable tracemalloc to get the object allocation traceback
Error
Traceback (most recent call last):
File "E:\PycharmProjects\pythonProject\workspace\Test_Calculator\main.py", line 27, in setUp
self.driver = webdriver.Remote(
^^^^^^^^^^^^^^^^^
File "E:\PycharmProjects\pythonProject\workspace\Test_Calculator\venv\Lib\site-packages\appium\webdriver\webdriver.py", line 267, in __init__
super().__init__(
File "E:\PycharmProjects\pythonProject\workspace\Test_Calculator\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "E:\PycharmProjects\pythonProject\workspace\Test_Calculator\venv\Lib\site-packages\appium\webdriver\webdriver.py", line 357, in start_session
response = self.execute(RemoteCommand.NEW_SESSION, parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\PycharmProjects\pythonProject\workspace\Test_Calculator\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "E:\PycharmProjects\pythonProject\workspace\Test_Calculator\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: socket hang up
Ran 1 test in 10.088s
FAILED (errors=1)

Creating new python Class with Selenium webdriver

Good morning.
I'm new to Selenium and I have some issues with Selenium 4.
Basically I'm trying to create a class that will have some custom methods like sending a request to specified URL.
But...I'm stuck at init method.
With the code I wrote the browser is starting but it breaks with error message:
====== WebDriver manager ====== Current google-chrome version is 101.0.4951 Get LATEST chromedriver version for 101.0.4951 google-chrome Driver
[/home/agent2/.wdm/drivers/chromedriver/linux64/101.0.4951.41/chromedriver]
found in cache Traceback (most recent call last): File
"/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py",
line 71, in start
self.process = subprocess.Popen(cmd, env=self.env, File "/usr/lib/python3.10/subprocess.py", line 966, in init
self._execute_child(args, executable, preexec_fn, close_fds, File "/usr/lib/python3.10/subprocess.py", line 1842, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/agent2/1-Python/Selenium-Scrapy/bot/booking/booking.py", line
29, in
a = MyBot() File "/home/agent2/1-Python/Selenium-Scrapy/bot/booking/booking.py", line
17, in init
super(MyBot, self).init() File "/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py",
line 70, in init
super(WebDriver, self).init(DesiredCapabilities.CHROME['browserName'], "goog",
File
"/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py",
line 90, in init
self.service.start() File "/home/agent2/1-Python/Selenium-Scrapy/venv/lib/python3.10/site-packages/selenium/webdriver/common/service.py",
line 81, in start
raise WebDriverException( selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
executable needs to be in PATH. Please see
https://chromedriver.chromium.org/home
This is my code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class MyBot(webdriver.Chrome):
"""My class"""
def __init__(
self,
driver=webdriver.Chrome(
service=Service(ChromeDriverManager().install()),
options = webdriver.ChromeOptions()
)
):
self.driver = driver
super(MyBot, self).__init__()
def land_first_page(self):
"""My custom method"""
self.get('My-url')
a = MyBot()
a.land_first_page()
To be honest I don't know how to solve this... Found some solutions but with Selenium 3 where you could simply pass a path to driver location.
Thanks!
This is because your class inherits from webdriver.Chrome, WebDriver __init__ tries to start a service using the chromedriver.exe file, but since you didn't provide a path it uses the default value executable_path="chromedriver" which doesn't exists in your project.
Just remove the inheritance and use the driver instance ChromeDriverManager().install() creates for you.

Cant run Selenium on Ubuntu with Python

I cant run or install selenium on Ubuntu 18.04.3 ...
I followed this tutorial: https://medium.com/#hoppy/how-to-test-or-scrape-javascript-rendered-websites-with-python-selenium-a-beginner-step-by-c137892216aa also with the digitalocean setup.
I tried different things...
I installed the geckodriver in /var/bin
in /var/local/bin
added both to PATH
tried with a path in the code and without
tried with firefox (preferred) ...
nothing helps
This Code:
import time
from selenium import webdriver
driver = webdriver.Chrome('/usr/bin/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()
Ends up with:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
selenium.common.exceptions.WebDriverException:: command not found
(venv) root#EtherScrape:/var/test# python /var/test/server_test.py Traceback (most recent call last):
File "/var/test/server_test.py", line 5, in <module>
driver = webdriver.Chrome('/usr/bin/chromedriver') # Optional argument, if not specified will search path.
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=83.0.4103.61 (94f915a8d7c408b09cc7352161ad592299f384d2-refs/branch-heads/4103#{#561}),platform=Linux 4.15.0-66-generic x86_64)
This Code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(firefox_options=options)
With that error:
Traceback (most recent call last):
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/test/server_test.py", line 6, in <module>
driver = webdriver.Firefox(firefox_options=options)
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x7ff3109ced68>>
Traceback (most recent call last):
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 163, in __del__
self.stop()
File "/var/venv/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Please help.
Tried many solutions so far but no one worked out...
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
Works for me at the chrome driver.

Selenium Chromedriver Won't Start

I'm trying to test my code using selenium with chrome driver. But I always got this error message in my terminal.
======================================================================
ERROR: test_input_status (myweb.tests.StorySixFunctionalTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/fredypasaud/Documents/PPW/story_six/myweb/tests.py", line 58, in setUp
self.selenium = webdriver.Chrome('./chromedriver',chrome_options = chrome_options)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/fredypasaud/Documents/PPW/django01/lib/python3.6/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 normally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /snap/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
And i put my chromedriver in the same directory as the manage.py of my project. And here's some code about from my test.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
class StorySixFunctionalTest(TestCase):
def setUp(self):
chrome_options = Options()
self.selenium = webdriver.Chrome('./chromedriver',chrome_options = chrome_options)
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('disable-gpu')
chrome_options.addArguments("--disable-extensions");
chrome_options.addArguments("--disable-dev-shm-usage");
super(StorySixFunctionalTest, self).setUp()
def tearDown(self):
self.selenium.quit()
super(StorySixFunctionalTest, self).tearDown()
def test_input_status(self):
selenium = self.selenium
selenium.get('http://127.0.0.1:8000/index/')
status = selenium.find_element_by_id('id_status')
submit = selenium.find_element_by_id('submit')
status.send_keys("Coba Coba")
submit.send_keys(Keys.RETURN)
Update :
I'm trying to debug my program by showing the log of chromedriver, and i got this error instead (which is weird because i have canbera module installed)
Gtk-Message: 14:40:58.177: Failed to load module "canberra-gtk-module"
Gtk-Message: 14:40:58.196: Failed to load module "canberra-gtk-module"

Login on Instagram using python but it gives error

I have made a code where I try to login to my instagram account but it gives me many errors. How can I fix them?
The code:
import pdb
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('https://www.instagram.com/accounts/login/')
dom = driver.find_element_by_xpath('//*')
pdb.set_trace()
username = dom.find_element_by_name('username')
password = dom.find_element_by_name('password')
login_button = dom.find_element_by_xpath('//*[#class="_qv64e _gexxb _4tgw8 _njrw0"]')
username.clear()
password.clear()
username.send_keys('your username')
password.send_keys('your password')
login_button.click()
driver.get('https://www.instagram.com/accounts/login')
if 'logged-in' in driver.page_source:
print 'Logged in'
The error:
Warning (from warnings module):
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 49
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
Traceback (most recent call last):
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in init
restore_signals, start_new_session)
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] El sistema no puede encontrar el archivo especificado
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Usuario\Desktop\Programaciones Python\Pruebas.py", line 4, in
driver = webdriver.PhantomJS()
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 56, in init
self.service.start()
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH.
The error message is telling you exactly what's wrong here: Message: 'phantomjs' executable needs to be in PATH.
You can fix this by locating your phantomjs.exe file -- wherever you downloaded phantomjs driver -- and adding it to your Path environment variable. This guide will help get you started in fixing this. The issue does not need to be fixed in your code, unless you want to manually pass the path to phantomjs.exe into your driver = webdriver.PhantomJS() call.

Categories