2013-10-09 50 views
0

我想熟悉硒。我决定尝试与facebook合作,并通过selenium完成我的个人资料。但他们使用太多ajax。完成我的家乡等不是很复杂,但我真的不知道如何上传图像。他们有一个ajax表单可供选择上传和网络摄像头照片。然后我需要以某种方式处理上传对话框......任何想法?使用Python和Selenium上传图像通过ajax形式

+0

http://stackoverflow.com/questions/8665072/how-to-upload-file-picture-with-selenium-python? – alexvassel

+0

它很相似但不一样......在那里他们只是指定图像的路径并点击上传。你可以去脸书,看看他们如何处理它。这是一个Ajax形式,然后我得到一个对话框来选择一个文件...但是,thanx,现在我有一个起点:) –

+1

正如我所看到的,页面源代码中有一个'fbTimelineFileForm'形式。可以用javascript提交。 – alexvassel

回答

1

所以好吧...这比我想象的要容易得多。我所需要的只是等待带有文件输入的ajax盒子,然后只需要send_keys与图像位置。

 try: 
      self.driver.find_element(By.CLASS_NAME, u"sx_53a53c").click() 
      WebDriverWait(self.driver, 10).until(ec.presence_of_element_located((By.CLASS_NAME, u"fbTimelineSelectorFileInput"))) 
      WebDriverWait(self.driver, 10).until(ec.presence_of_element_located((By.NAME, u"pic"))) 
      self.driver.find_element(By.NAME, u"pic").send_keys("~/Downloads/z_ed6e1de4.jpg") 
     except NoSuchElementException as nse: 
      print 'Error. Element not found! '.format(nse.message) 
     except: 
      print "Something went wrong." 
      import traceback 
      type_, value_, trace_ = sys.exc_info() 
      print traceback.format_tb(trace_) 
+0

很高兴我的建议很有帮助。 – alexvassel

+0

@alexvassel yup :) thanx! –