How do I click Enter using Splinter? - python

Here is my Splinter code -
b = Browser()
b.visit("http://boingboing.net")
b.fill("q", "OpenXC")
At this point, I would like to press "Enter" for the search to happen.
This is very similar to the example in the Splinter tutorial page. In this instance, there is really no button element present.
I see that this Search box is a Google Search Box with an id "cse-search-box". I do not know how to trigger search here.
Can you please help?
Metaphy's (thanks, btw) code below generates a traceback for me -
Traceback (most recent call last):
File "/Users/muthu/Desktop/boing.py", line 5, in
b.execute_script('document.getElementsByName("f")[0].submit()')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/splinter/driver/webdriver/init.py", line 58, in execute_script
self.driver.execute_script(script)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 397, in execute_script
{'script': script, 'args':converted_args})['value']
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'waiting for evaluate.js load failed' ; Stacktrace:
at r (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:8360)
at fxdriver.Timer.prototype.runWhenTrue/g (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:392)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/5r/06jrtyyn2kd8ty05m71lm0hr0000gn/T/tmpBFCIVe/extensions/fxdriver#googlecode.com/components/driver_component.js:386)

I can not access the http://boingboing.net site, so I take baidu.com for example:
from splinter.browser import Browser
b = Browser('firefox')
b.visit('http://www.baidu.com')
b.fill('wd', 'test')
b.execute_script('document.getElementsByName("f")[0].submit()')
New answer:
browser.fill('some_name', 'some_value\r')

Related

Python WebDriver send_keys not working on element and returns ElementNotInteractableException

I am trying to send some keystrokes using python webdriver to a p element that is attached to a js event listener. When i type the keys in manually, it works. However when I use driver.send_keys(), it returns an ElementNotInteractableException. The element is as follows:
<p data-v-6779462c="" id="textBox" class="input"></p>
My code to locate and send the keystrokes to the element is as follows:
input_area = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//p[#id="textBox"]')))
input_area.click()
input_area.send_keys(keys)
input_area.send_keys(Keys.ENTER)
The full stacktrace is as follows:
Traceback (most recent call last):
File "automation.py", line 32, in <module>
input_area.send_keys(word)
File "/home/server/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "/home/server/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/home/server/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/server/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=87.0.4280.66)
I am using Chrome on Ubuntu Linux.
I need the way to solve this and why it is happening.
Thanks in advance!
P tag is a paragraph tag , you cannot Interact with it .
You should be sending keys to input tag
Try below code
input_area.click()
elemInput = driver.switch_to.active_element
elemInput.send_keys(Keys.ENTER)

Pasting a data on a new Google tab

I am new in programming (specifically Python Selenium), I have a question regarding pasting a data into an input box, it seems not working if I will paste data on another website, I will received an error. But if I use just one website (either of the two) in pasting the data it is working fine.
Here is my codes below.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Users\Pashu\AppData\Local\Programs\Python\Python37\chromedriver.exe')
driver.get('https://tinyurl.com/')
inline_tinysite = driver.find_element_by_xpath('//*[#id="url"]')
inline_tinysite.send_keys("https://www.teamxtools.com/content/images/project-7/stack-324/exif/2dehands-velgen-volkswagen-golf-7.jpg")
submit = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[2]/div[2]/form/input[3]')
submit.click()
driver.execute_script("window.open('https://cutt.ly/', 'new window')")
inline_cuttly = driver.find_element_by_xpath('//*[#id="link"]')
inline_cuttly.send_keys("https://www.teamxtools.com/content/images/project-7/stack-324/exif/2dehands-velgen-volkswagen-golf-7.jpg")
submit2 = driver.find_element_by_xpath('/html/body/div[1]/section[1]/div/div/div/div/div/button')
submit2.click()
#time.sleep(8)
Here is the error message as text:
C:\Users\Pashu\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Pashu/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/Helloworld.py
Traceback (most recent call last):
File "C:/Users/Pashu/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/Helloworld.py", line 14, in <module>
inline_cuttly = driver.find_element_by_xpath('//*[#id="link"]')
File "C:\Users\Pashu\PycharmProjects\untitled\venv\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\Pashu\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Pashu\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Pashu\PycharmProjects\untitled\venv\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="link"]"}
(Session info: chrome=81.0.4044.138)
Process finished with exit code 1
it was stuck on window 1, that is why element //*[#id="link"] is not found. I just added
driver.switch_to.window(driver.window_handles[1])
for it to search on the second window.

Python click Js link with selenium

Iam tested webscraping the page:http://www.guiadosquadrinhos.com/todas-capas-disponiveis
I need navigate in pagination geting info pages favorite.
I need click link javascript next page:
javascript:__doPostBack('ctl00$MainContent$lstProfileView$dataPagerNumeric2$ctl02$ctl00')
from selenium import webdriver
import time
driver = webdriver.PhantomJS(executable_path='C:\Python27\Tools\phantomjs\phantomjs.exe')
driver.get("http://www.guiadosquadrinhos.com/todas-capas-disponiveis")
#print(driver.find_elements_by_class_name("numero_capinha")[0].text)
#driver.find_elements_by_class_name("next_last")[0].click()
#time.sleep(5)
print(driver.find_elements_by_class_name("numero_capinha")[0].text)
driver.find_elements_by_class_name("next_last")[0].click()
print(driver.find_elements_by_class_name("numero_capinha")[0].text)
My code return:
sobreontem - Independente
Traceback (most recent call last): File "teste_selenium.py", line 10, in
driver.find_elements_by_class_name("next_last")[0].click() File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 74, in click
self._execute(Command.CLICK_ELEMENT) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 453, in _execute
return self._parent.execute(command, params) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 201, in execute
self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 181, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotVisibleException: Message:
{"errorMessage":"Element is not currently visible and may not be
manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:63160","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\":
\"5d3cfdc0-5d3b-11e5-b784-67706273a0bb\", \"id\":
\":wdc:1442494581220\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/5d3cfdc0-5d3b-11e5-b784-67706273a0bb/element/%3Awdc%3A1442494581220/click"}}
Screenshot: available via screen
Whats is problem ?
Since, you are doing the following -
driver.find_elements_by_class_name("next_last")[0].click()
If you look in the source, there are multiple elements with this class name and the first one is disabled, because that is for the previous button and you are on first page.

Python + Selenium to submit query on webpage

Hello all…I am trying to use Python and Selenium to submit query, a pair of coordinates “40.67243, -74.08379” on Google Map.
driver = webdriver.PhantomJS()
driver.get('https://maps.google.com/')
driver.find_element_by_id("searchboxinput").clear()
driver.find_element_by_id("searchboxinput").send_keys("40.67243, -74.08379")
driver.find_element_by_css_selector("button.searchbutton").click()
using above returns chunk of error message and problem starts from:
driver.find_element_by_id("searchboxinput").clear()
when I check it from Chrome, and seems there’s no an element “searchboxinput”. Is it the reason?
What’s the way to submit query? Thanks.
the error:
Traceback (most recent call last):
File "C:\Python27\Gmap.py", line 10, in <module>
driver.find_element_by_id("searchboxinput").clear()
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 197, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 681, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 164, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\selenium\webdriver\remote\errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'{"errorMessage":"Unable to find element with id \'searchboxinput\'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"95","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:62738","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"using\\": \\"id\\", \\"sessionId\\": \\"e6a0af20-4f91-11e4-a8b7-7b4551650c5b\\", \\"value\\": \\"searchboxinput\\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/e6a0af20-4f91-11e4-a8b7-7b4551650c5b/element"}}' ; Screenshot: available via screen
Everithing seems fine. Exception says that there is no element with id searchboxinput. So maybe you confused id with class or something like that?

Selenium click() from link text throwing exception on Windows

Using python from command line, I execute the following:
driver = webdriver.Firefox()
driver.get('http://amazon.com/mp3')
el = driver.find_element_by_link_text('MP3 Downloads : MP3 Songs')
el.click()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\selenium-2.19.1-py2.7.egg\selenium\webdriver\remote\webelement.py", line 45, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium-2.19.1-py2.7.egg\selenium\webdriver\remote\webelement.py", line 194, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium-2.19.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.19.1-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: u'current is undefined'
I have no idea what "CURRENT" is...
I ran it on a different machine and the error became
...Message u'l is undefined'
Any suggestions? By the way this only happens for Windows on Firefox.
I've had the same issue when clicking a button.
You can click it by sending the "space" character:
webDriver.FindElement("button_id").SendKeys(" ");
Hope it helps!

Categories