I'm trying to upload a file to a website using python and selenium. I tried following methods
driver = webdriver.Firefox()
driver.get("ip-address")
method 1:
test = driver.find_element_by_id("selectedFile")
test.send_keys('/Users/knightfox/Desktop/file.txt')
method 2:
test = driver.find_element_by_xpath("//input[#type='file']")
test.send_keys('/Users/knightfox/Desktop/file.txt')
method 3:
test = driver.find_element_by_css_selector('input[type="file"]')
test.send_keys(r'/Users/knightfox/Desktop/file.txt')
But I get following errors when execute.
Traceback (most recent call last):
File "/home/knightfox/Desktop/bell/sel.py", line 18, in <module>
test.send_keys(r/Users/knightfox/Desktop/file.txt)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="selectedFile" name="file" type="file"> is not reachable by keyboard
I'm using Python 2.7. Following is the html page.
Update:
Following code by Ian solved the issue.
field = driver.find_element_by_id("selectedFile")
driver.execute_script("arguments[0].style.display = 'block';", field)
field = driver.find_element_by_id("selectedFile")
field.send_keys('/Users/knightfox/Desktop/file.txt')
The file field is hidden by its style="display: none;". Before you can interact with it, you need to make it visible.
field = driver.find_element_by_id("selectedFile")
driver.execute_script("arguments[0].style.display = 'block';", field)
field.send_keys('/Users/knightfox/Desktop/file.txt')
Related
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.
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.
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?
I have a textfield on a web page that I want to feed data with but the problem is my python-selenium script is not able to locate it when it runs and reaches that page. The weird part is when i try to locate this textfield manually, I am able to do so without any problem. I am using CSSSelector to locate this field.
Stack trace:
Traceback (most recent call last):
File "C:/SWInstallation/TestCases/TestCases\Program.py", line 23, in test_UC_QS_FR_01_1_QuikShip_Support
bhp.enterAccDetails("0004341080", "00200")
File "C:\SWInstallation\HC-Branch\src\HomePage.py", line 24, in enterAccDetails
accInputField = self.driver.find_element_by_css_selector("#fields input[name='ACCOUNT_NUMBER']")
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 366, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 680, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\selenium\webdriver\remote\errorhandler.py", line 158, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u"Unable to find element with css selector == #fields input[name='ACCOUNT_NUMBER']"
Have you tried something like this:
e = self.driver.find_element_by_id("fields")
inputs = e.find_elements_by_css_selector("input[name='ACCOUNT_NUMBER']")
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!