send_keys() giving error in python 3 selenium code with firefox - python-3.x

My code is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
baseurl = "https://www.google.ca/?gfe_rd=cr&ei=J5ooWerXOsf_8AebtKKICw&gws_rd=ssl"
search = "panda"
xpaths = { 'searchbox' : ".//*[#id='lst-ib']",
'submit' : ".//*[#id='tsf']/div[2]/div[3]/center/input[1]",
'img' : ".//*[#id='gbw']/div/div/div[1]/div[2]/a"
}
driver = webdriver.Firefox()
driver.get(baseurl)
driver.find_element_by_xpath(xpaths['searchbox']).clear()
driver.find_element_by_xpath(xpaths['searchbox']).send_keys(search)
driver.find_element_by_xpath(xpaths['submit']).click()
#driver.find_element_by_xpath(xpaths['img']).click()
Firefox opens, but nothing at all happens, and written in the terminal is the following:
Traceback (most recent call last):
File "sg1.py", line 21, in <module>
driver.find_element_by_xpath(xpaths['searchbox']).send_keys(search)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py", line 347, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Expected [object Undefined] undefined to be a string
Things to note:
Firefox is up to date.
When Firefox opens it opens a plain version and not the version that usually opens with add-ons such as adblocker, firebug, etc.
When i ran just the click on 'img' bit that is commented out it did what it was supposed too.

This issue is common with geckodriver v.015; In order to resolve this update your geckodriver version to 0.16 also selenium to 3.4.0.

Related

Why can't run geckodriver in selenium?

Download chromedriver and extract it.
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/home/debian/Downloads/chromedriver')
It works fine.
Now to test geckodriver for firefox,my firefox version is 78.0.2 (64-bit),the latest version,i have downloaded the geckodriver-v0.26.0-linux64.tar.gz and extract it.
from selenium import webdriver
driver = webdriver.Firefox(executable_path='/home/debian/Downloads/geckodriver')
It encounter some issues:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-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
How to fix it?
first check if selenium are downloaded with no error and make sure that the webdriver are downloaded in the path in this code driver = webdriver.Firefox(executable_path='/home/debian/Downloads/geckodriver')
that's should fix your problem

My selenium program isn't able to click on an input box

I have made a program to grab a youtube link and type it on a website but its not able to find the input box...plz tell me why
import time
from selenium import webdriver
import pyautogui as auto
url = 'https://www.youtube.com/results?search_query=closer+8d'
web = webdriver.Chrome('chromedriver.exe')
web.get(url)
time.sleep(3)
link = web.find_element_by_id('video-title')
link = link.get_attribute('href')
auto.hotkey('ctrl', 't')
auto.typewrite('https://www.youtubemp3.pro/en2')
auto.press('enter')
time.sleep(3)
search_bar = web.find_element_by_xpath('//*[#id="videoURL"]')
search_bar.click()
search_bar.send_keys(link)
The error I'm getting is:
Traceback (most recent call last):
File "e:\pranil\python\doing shit with python\songs downloader.py", line 17, in <module>
search_bar = web.find_element_by_xpath('//*[#id="videoURL"]')
File "C:\Users\Pranil.DESKTOP-TLQKP4G.000\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Pranil.DESKTOP-TLQKP4G.000\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Pranil.DESKTOP-TLQKP4G.000\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Pranil.DESKTOP-TLQKP4G.000\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="videoURL"]"}
(Session info: chrome=83.0.4103.116)
The xpath you provided to find an element doesn't seem to work ""//[#id="videoURL"]"" I also opened up a YouTube video to see what you were looking for but I couldn't find videoURL if you are trying to get the URL I recommend you have selenium click on the share button and grab the link from there.
import time
from selenium import webdriver
import pyautogui as auto
url = 'https://www.youtube.com/results?search_query=closer+8d'
web = webdriver.Chrome('./chromedriver')
web.get(url)
time.sleep(3)
link = web.find_element_by_id('video-title')
link = link.get_attribute('href')
web.execute_script("window.open('https://www.youtubemp3.pro/en2', 'new_window')")
web.switch_to_window(web.window_handles[-1])
ylink = web.find_element_by_id("videoURL")
ylink.click()
ylink.send_keys(link)
submit = web.find_element_by_name("submitForm")
submit.click()
you do not have to use pyautogui it will work better if you use seleniu
try to find the element with it's ID.
import time
from selenium import webdriver
url = 'https://www.youtube.com/results?search_query=closer+8d'
web = webdriver.Chrome('../assets/chromedriver')
web.get(url)
time.sleep(3)
link = web.find_element_by_id('video-title')
link = link.get_attribute('href')
web.get('https://www.youtubemp3.pro/en2/')
time.sleep(3)
search_bar = web.find_element_by_id('txt-url')
search_bar.click()
search_bar.send_keys(link)

"Unable to find a matching set of capabilities" using selenium/geckodriver for Firefox on Windows10

Versions:
geckodriver 0.26.0 (in PATH environment variable)
selenium 3.141.0
Firefox 75.0
python 3.8.2
Windows 10
When I try:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://seleniumhq.org/')
I get this:
Traceback (most recent call last):
File "C:\Users\Me\Desktop\python\FFtests.py", line 3, in <module>
driver = webdriver.Firefox()
File "C:\Users\Me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "C:\Users\Me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\Me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Me\AppData\Local\Programs\Python\Python38-32\lib\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
When I try:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False
driver = webdriver.Firefox(capabilities = cap)
driver.get('https://seleniumhq.org/')
I get this:
Traceback (most recent call last):
File "C:\Users\me\Desktop\python\FFtests.py", line 6, in <module>
driver = webdriver.Firefox(capabilities = cap)
File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 190, in __init__
executor = ExtensionConnection("127.0.0.1", self.profile,
File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 73, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 103, in _wait_until_connectable
raise WebDriverException(
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.
IEDriver works fine for IE
I have Windows Visual Studio installed as https://github.com/mozilla/geckodriver/releases indicates I should.
What can I do to make this work?
This is likely an issue with FireFox version. Try updating Firefox and Selenium.
Update Firefox
Update Selenium
Reinstall/update Geckodriver
See here for more solutions.

Error while executing javascript code in python3 while using selenium

I was trying to remove the ad and other pop-up using selenium after opening the webpage. The pop-up is getting removed but the ad is not getting removed. There is some error in executing javascript code(using it remove ad). For which there is no reason given. Also, for the ad, when I open the webpage myself by typing the link then ad does not appear but when I run the program, in code generated browser ad appears(reason I don't know). I have attached both code and error. Here is the code
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("http://indianexpress.com/columnists/")
// To remove pop-up
later = driver.find_elements(By.CLASS_NAME, "iz_block_button")
later[0].click()
//to remove ad
all_iframes = driver.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
print("Ad Found\n")
driver.execute_script("""
var elems = document.getElementsByTagName('iframe');
for(var i = 0, max = elems.length; i < max; i++)
{
elems[i].visibility=hidden;
}
""")
print('Total Ads: ' + str(len(all_iframes)))
else:
print('No frames found')
driver.close()
Error:-
Traceback (most recent call last):
File "/Users/arjungoyal/Desktop/untitled/a.py", line 23, in <module>
""")
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 627, in execute_script
'args': converted_args})['value']
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/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: hidden is not defined
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.4 x86_64)
Please someone tell what the error is and how to remove it.

how to add extentions in Fireforx web driver using selenium and python

I want to add canvasBlocker extension to Firefox webDriver, I had download the extension in a xpi format but i did not understand why this code not working
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium import webdriver
profile = FirefoxProfile()
profile.add_extension(extension="canvasblocker-0.4.5b-an+fx.xpi")
profile.set_preference("extensions.canvasblocker.currentVersion","0.4.5b")
driver = webdriver.Firefox(firefox_profile=profile,executable_path="/home/user/Bureau/doc/geckodriver")
Error -
File "scraping.py", line 31, in
profile.add_extension(extension="canvasblocker-0.4.5b-an+fx.xpi") File
"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_profile.py",
line 95, in add_extension
self._install_extension(extension) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_profile.py",
line 274, in _install_extension
addon_details = self._addon_details(addon) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_profile.py",
line 351, in _addon_details
raise AddonFormatError(str(e), sys.exc_info()[2]) selenium.webdriver.firefox.firefox_profile.AddonFormatError: ("[Errno
2] No such file or directory:
'/tmp/tmp92ife_.canvasblocker-0.4.5b-an+fx.xpi/install.rdf'",
)
You can read about the reason for the error and a workaround here:
USING FIREFOX WEBEXTENSIONS WITH SELENIUM

Resources