Selenium send_keys 无响应
Selenium send_keys with no response
当我 运行 我的代码使用 PhantomJS 和 selenium 时,res 显示 act,但是在 send_keys 代码不会继续,只是停留在,没有错,没有任何答案没有保留。我只想知道为什么。
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')
这是因为您只选择了文件,没有提交任何表格,也没有采取任何行动。因此,您需要单击某个提交按钮或使用您的文件路径发送 ENTER 键,如下所示:
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)
而且你真的不应该使用正则表达式来查找输入,你最好尝试 xpath 或者可能是 css 选择器。
在切换到无头模式之前,使用 Firefox 等图形浏览器进行调试是一种很好的方法(Google Chrome --headless 或 PhantomJS,如您的情况)。
当我 运行 我的代码使用 PhantomJS 和 selenium 时,res 显示 act,但是在 send_keys 代码不会继续,只是停留在,没有错,没有任何答案没有保留。我只想知道为什么。
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')
这是因为您只选择了文件,没有提交任何表格,也没有采取任何行动。因此,您需要单击某个提交按钮或使用您的文件路径发送 ENTER 键,如下所示:
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)
而且你真的不应该使用正则表达式来查找输入,你最好尝试 xpath 或者可能是 css 选择器。
在切换到无头模式之前,使用 Firefox 等图形浏览器进行调试是一种很好的方法(Google Chrome --headless 或 PhantomJS,如您的情况)。