(at least)
If the display
of the ʻinput tag is
none`, the file cannot be set properly.
So, before specifying the file, it works well if you play with the style and visualize it.
driver.execute_script("document.getElementsByName('datafile')[0].style.display = '';")
Don't worry if it's not 3 or Headless Chrome.
The verification site is
<INPUT type =" file ">
-HTML tag reference
I will use ʻinput` of.
Successful guy
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
if __name__ == '__main__':
try:
driver = webdriver.PhantomJS()
driver.get('http://www.htmq.com/html/input_file.shtml')
elm = driver.find_element_by_name("datafile")
print('get')
elm.send_keys('./test.jpg')
print('send')
except TimeoutException as e:
print(e)
finally:
driver.quit()
The one who fails
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
if __name__ == '__main__':
try:
driver = webdriver.PhantomJS()
driver.get('http://www.htmq.com/html/input_file.shtml')
# new!
driver.execute_script("document.getElementsByName('datafile')[0].style.display = 'none';")
elm = driver.find_element_by_name("datafile")
print('get')
elm.send_keys('./test.jpg')
print('send')
except TimeoutException as e:
print(e)
finally:
driver.quit()
In this example, something that works normally is intentionally prevented from moving.
On the other hand, if something fails = style.display ='none'
, you have to remove it.
driver.execute_script("document.getElementsByName('datafile')[0].style.display = '';")
The site I encountered was making a button with a div containing a hidden input. It worked fine both manually and in the Selenium IDE, so it took a lot of time to identify the problem ...
Recommended Posts