Python Splinter cannot click button - python

I am running through the Splinter tutorial located here: http://splinter.readthedocs.io/en/latest/tutorial.html#
The code I am using:
from splinter import Browser
browser = Browser()
browser.visit('http://google.com')
browser.fill('q', 'chicago pizza')
button = browser.find_by_name('btnG')
button.click()
Every time I try to get my code to click the search I get the following error:
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
button.click()
File "C:\Python27\lib\site-packages\splinter\driver\webdriver\__init__.py", line 546, in click
self._element.click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 77, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotInteractableException: Message:
I have used browser.find_by_id, browser.find_by_tag, browser.find_by_text and none have worked, all giving me the same error.
I am running python 2.7.8 Firefox v 54.0.1 (32-bit) and have selenium installed.
Any idea of how to fix this? I think it may have to do with my geckodriver.
I would like to solve the problem and not have to work around it by say switching web browsers.

Everything worked fine except you have entered wrong name of button.
Here is the working code
from splinter import Browser
browser = Browser()
browser.visit('http://google.com')
browser.fill('q', 'chicago pizza')
button = browser.find_by_name('btnK')
button.click()

Related

How do I resolve this Selenium exception on a Mac thats says "chrome not reachable"?

I'm trying to learn how to automate web processes using Selenium and hopefully be able to build robust web scrapers and stuff. So, I just finished installing Pycharm and Selenium, and I am just trying to run a simple snippet of code that opens a web page in chrome, nothing too fancy. My code is as follows (it's in Python of course)
from selenium import webdriver
PATH = "/Users/Kudzie/Tools/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get('https://orbitrevolution.tech')
So , when I run this code, I get this exception, and I have no idea how to solve this, I've tried reading past variations of this question but i didn't seem to get a solution that works. the exception is as follows
Traceback (most recent call last):
File "/Users/Kudzie/PycharmProjects/Web Scraper/main.py", line 5, in <module>
driver = webdriver.Chrome(PATH)
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/Kudzie/PycharmProjects/Web Scraper/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
Process finished with exit code 1
Try replacing this:
driver = webdriver.Chrome(PATH)
With this:
driver = webdriver.Chrome(executable_path=PATH)

Unable to store a function in a variable in Selenium

Okay so I was writing my first selenium code which is:
browser.get('https://www.jetbrains.com/pycharm/')
browser.find_element_by_link_text('Download')
<selenium.webdriver.remote.webelement.WebElement (session="69f43bf4a5e98baf24ad3746dd01b514", element="0fa96685-d871-464f-aaaa-af02238a5f08")>
I wanted to simply go to the pycharm website and click on downloads, which worked fine.
But when I tried to store the previous function in a variable like so:
elem = browser.find_element_by_link_text('Download')
I got all these errors:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Users\admin\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\admin\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\admin\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.NoSuchWindowException: Message: no such window: window was already closed
(Session info: chrome=87.0.4280.66)
I have no idea what they mean or what ive done wrong. Help much appreciated.
PS: I am using pycharm and selenium chromedriver if thats helpful.
No issues in this.
from selenium import webdriver
browser = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')
browser.get('https://www.jetbrains.com/pycharm/')
element = browser.find_element_by_link_text('Download')
element.click()

How to click a button that appears as a label?

Image of the code and what the button looks like
Im trying to click that add button but I cant find anything that says button when i inspect element
Here is my python code im trying to get work but not sure how to do it
buttonclick = driver.find_elements_by_class_name('add-vehicle-container')
buttonclick.click()
time.sleep(1)
Errors im getting
Traceback (most recent call last):
File "C:/Users/ttttt/iCloudDrive/Documents/GitHub/SeniorProject2019TBA/Selenium/AboutYouTesting.py", line 50, in <module>
buttonclick.click()
File "C:\Users\ttttt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\ttttt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\ttttt\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\ttttt\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.ElementNotInteractableException: Message: Element <div class="add-vehicle-container"> could not be scrolled into view
Run one of these commands in the developer console.
getEventListeners($("#SomeElement")[0]); //Javascript
$._data($("#SomeElement")[0], "events"); //JQuery
These will tell you if the events attached are indeed "click" events, or some other type of event.
If it is indeed a click event, you can try this:
driver.execute_script("$('.add-vehicle-container').click()");
If that does not work, or the events are not click events, then you can run this:
driver.execute_script("$('.add-vehicle-container').trigger('ATTACHED_EVENT_NAME_HERE')");
You can play around with these to see if they work by simply running them in the browser developer console before trying them in your python script.

Run selenium on pythoanywhere.com

I've wrote a code and works perfectly on my computer, then i decided to move it on a server cloud, and then I create an account on pythonanywhere.com
Then, after a lot of try I understand that chromedriver is not working over there, but just firefox, no big deal .
Then I did some test and works fine, but when I tried to use:
browser.find_element_by_link_text('Log in').click()
It doesn't work
well, my code is:`
from pyvirtualdisplay import Display
from selenium import webdriver
from time import sleep
with Display():
browser = webdriver.Firefox()
try:
browser.get('https://www.instagram.com')
print("get instagram.com ")
browser.implicitly_wait(60)
browser.find_element_by_link_text('Log in').click()
print("click log in")
sleep(10)
finally:
browser.quit()
the error is :
get instagram.com
Traceback (most recent call last):
File "/home/fobu36/.virtualenvs/prova_selenium.py", line 16, in <module>
browser.find_element_by_link_text('Log in').click()
File "/home/fobu36/.local/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", l
ine 317, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "/home/fobu36/.local/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", l
ine 752, in find_element
'value': value})['value']
File "/home/fobu36/.local/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", l
ine 236, in execute
self.error_handler.check_response(response)
File "/home/fobu36/.local/lib/python3.6/site-
packages/selenium/webdriver/remote/errorhandler.py"
, line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
element: {"method":"l
ink text","selector":"Log in"}
Stacktrace:
at FirefoxDriver.findElementInternal_
(file:///tmp/tmpbydkaxfk/extensions/fxdriver#googlecode.
com/components/driver-component.js:10770)
at fxdriver.Timer.setTimeout/<.notify
(file:///tmp/tmpbydkaxfk/extensions/fxdriver#googlecode.
com/components/driver-component.js:625)
seems like that it can't find the log in button, but i'm sure that in local it's working

Selenium error At the end of the page

I have written this code using selenium python so parse a webpage(dynamically loading) so
that it goes till the end and then stop when "load more" button is not clickable anymore.
wait=WebDriverWait(driver,50)
wait.until(EC.element_to_be_clickable((By.ID, 'bottomPager'))) while
EC.element_to_be_clickable((By.ID,'bottomPager')):
driver.find_element_by_xpath('.//div[#id="bottomPager"]').click()
if not driver.find_element_by_id('bottomPager').is_enabled():
break
wait.until(EC.element_to_be_clickable((By.ID,'bottomPager')))
but I am getting the error when it reaches the end of the page
Traceback (most recent call last): File "python_org_search2.py", line 24, in <module>
driver.find_element_by_xpath('.//div[#id="bottomPager"]').click() File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 60, in click
self._execute(Command.CLICK_ELEMENT) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 370, in _execute
return self._parent.execute(command, params) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 166, in execute
self.error_handler.check_response(response) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: u'unknown error: Element is not clickable at point (643, 628)\n (Session info: chrome=34.0.1847.137)\n (Driver info: chromedriver=2.9.248304,platform=Linux 3.13.0-24-generic x86_64)'
What are the changes required in code so as to rectify it ?
In my experience, this exception only occurs when using the Chrome driver with Selenium. Can you use Firefox instead? (I believe Firefox is the primary intended browser for webdriver invocations via Selenium.)
driver = webdriver.Firefox()
That should clear up this exception.
(You can get portable versions of Firefox at this site.)

Categories

Resources