梁生珺 梁生珺 - 1 year ago 68
Python Question

Selenium send_keys with no response

When I run my code with PhantomJS and selenium ,and the res shows act, but in send_keys the code won't go on, just stay in, no wrong , no any answer and no keep in. I just want know why.

res = re.findall(r'\<input id=\"([^\n]*)\" type=\"file\" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;',pages)
dr = driver.find_element_by_id(res[0])
dr.send_keys('/Users/liangshengjun/Desktop/bin/2_0.jpg')

Answer Source

It happens because you've only selected file, you don't submit any form and didn't take any action. So you need either to click some submit button or send ENTER key with your file path like this:

from selenium.webdriver.common.keys import Keys
# your code here, then add this key on last step.
dr.send_keys('/Users/liangshengjun/Desktop/bin/2_0.jpg' + Keys.ENTER)

And you really shouldn't use regexes to find an input, you'd better try xpath or may be css selectors.

And it's good approach to debug with some graphic browser like Firefox before switching to headless mode (Google Chrome --headless or PhantomJS as in your case).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download