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.
Related
I am trying to scroll to the bottom of the page, as I usually do. But on this website it seems not to work. It's one of those where you need to scroll to the bottom in order to load new links.
This is the command I am using:
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
The error I am getting is:
Traceback (most recent call last):
File "/somePathTo/someProgram.py", line 12, in <module>
browser.execute_script("window.scrollTo(0,document.body.scrollHeight);")
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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 read property 'offsetTop' of null
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Mac OS X 10.14.3 x86_64)
Any other scroll does not work as well, something is wrong with the JS script...
I don't know what element is null and therefore can't have the 'offsetTop' property... maybe I should identify some element that can have that property, but I don't know much more about JS than this script
EDIT:
I did solve my specific problem with this code
browser.execute_script('arguments[0].scrollIntoView(true);', target)
where target is an element to the bottom. However, I would like to know what the issue is with the JS code.
I'm new to python coding. And I'm trying to play around with python and Selenium in chrome. And I have this code where I'm trying to move a webpage up and down. But it isn't working.
body_elem = browser.find_element_by_tag_name('body')
for __ in range(3):
body_elem.send_keys(Keys.END)
sleep(2)
body_elem.send_keys(Keys.HOME)
sleep(2)
All I get is this long error message:
Traceback (most recent call last):
File "selenium_test.py", line 34, in <module>
body_elem.send_keys(Keys.HOME)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
Anybody got an idea of whats is wrong?
This is most likely because of the fact that the DOM tree or the body element signature itself changed after the first "down" move. One straightforward way to approach the problem would be to "re-find" the element constantly:
for _ in range(3):
browser.find_element_by_tag_name('body').send_keys(Keys.END)
sleep(2)
browser.find_element_by_tag_name('body').send_keys(Keys.HOME)
sleep(2)
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()
I want to delete youtube's watch later videos. but my codes don't work.
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-default yt-uix-button-empty yt-uix-button-has-icon no-icon-markup pl-video-edit-remove yt-uix-tooltip" type="button" onclick=";return false;" title="Remove"></button>
my code is this.
_sDriver.get('https://www.youtube.com/playlist?list=WL')
_sDriver.find_element_by_class_name('pl-video-edit-remove').click()
exception is this.
>>> _sDriver.find_element_by_class_name('pl-video-edit-remove')
<selenium.webdriver.remote.webelement.WebElement (session="283d423c-5ff7-478f-89
b9-002499413126", element="{e00dbb1e-e0ca-4f79-8652-23c955b464e7}")>
>>> _sDriver.find_element_by_class_name('pl-video-edit-remove').click()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 72, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 461, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 236, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not c
urrently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///c:/users/admini~1/appdata/local/t
emp/tmppqz9zq/extensions/fxdriver#googlecode.com/components/command-processor.js
:10092)
at DelayedCommand.prototype.checkPreconditions_ (file:///c:/users/admini~1/a
ppdata/local/temp/tmppqz9zq/extensions/fxdriver#googlecode.com/components/comman
d-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/admini~1/ap
pdata/local/temp/tmppqz9zq/extensions/fxdriver#googlecode.com/components/command
-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/admini~1/appd
ata/local/temp/tmppqz9zq/extensions/fxdriver#googlecode.com/components/command-p
rocessor.js:12666)
at DelayedCommand.prototype.execute/< (file:///c:/users/admini~1/appdata/loc
al/temp/tmppqz9zq/extensions/fxdriver#googlecode.com/components/command-processo
r.js:12608)
I don't know what can I do for this.
I need your help.
thank you.
The element that you are trying to click is not visible normally. It is only visible when you hover the mouse on it. So either you fire a mouseover event before clicking and try if it works.
The other solution is that you can try executing the JavaScript.
driver.execute_script("document.getElementsByClassName('pl-video-edit-remove')[0].click()")
The script is working at my end.
you may want to add some wait before executing the script so that the page loads properly.
I am attempting to click a button on a html page using Python and selenium web driver.
This is the source code of the page http://pastebin.com/112g1Gje.
EDIT: The relevant part at is at the end.. "btn btn-primary"
I am really close to figuring this out:
The class name had spaces in it and I replaced the spaces with the dots... that made it work (Thanks to another SO post).
driver.find_element_by_css_selector(".btn.btn-primary").click()
Now the error I get is:
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
So I am trying something like:
driver.execute_script('document.querySelector("btn.btn-primary").click()')
From my understanding I have to write a javascript that clicks the button instead because the element is not visible. But that doesn't work.
The error I get is almost unreadable:
driver.execute_script('document.querySelector("btn.btn-primary").click()')
File "/Users/Richie/anaconda/lib/python3.5/site- packages/selenium/webdriver/remote/webdriver.py", line 461, in execute_script
{'script': script, 'args':converted_args})['value']
File "/Users/Richie/anaconda/lib/python3.5/site- packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "/Users/Richie/anaconda/lib/python3.5/site- packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: document.querySelector(...) is null
Anyone know?
driver.find_element_by_css_selector(".btn.btn-lg.btn-block.btn-message.open-write-message").click()
I was clicking the wrong thing.
Make sure to include the dots.