WebView中响应网页的“上传文件”需求

第一步:

webView.getSettings().setJavaScriptEnabled(true);

第二步:

webView.setWebChromeClient(new WebChromeClient() {
//扩展浏览器上传文件
//3.0++版本
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
   openFileChooserImpl(uploadMsg, acceptType);
}

//3.0--版本
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
   openFileChooserImpl(uploadMsg, null);
}

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
   openFileChooserImpl(uploadMsg, acceptType);
}

// For Android > 5.0
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, FileChooserParams fileChooserParams) {
   String[] acceptTypes = fileChooserParams.getAcceptTypes();
   openFileChooserImplForAndroid5(uploadMsg, acceptTypes);
   return true;
}});

//acceptType即是web端要求的文件类型,一般有

图片:image/*
视频:video/*

图片:image/jpeg,image/gif,image/png

 

第三步:设置类型

private void openFileChooserImpl(ValueCallback<Uri> uploadMsg, String acceptType) {
   mUploadMessage = uploadMsg;
   Intent i = new Intent(Intent.ACTION_GET_CONTENT);
   i.addCategory(Intent.CATEGORY_OPENABLE);
   i.setType("*/*");
   if (acceptType != null) {
      i.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{acceptType});
   }
   startActivityForResult(Intent.createChooser(i, "文件选择"), FILECHOOSER_RESULTCODE);
}

private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg, String[] acceptTypes) {
   mUploadMessageForAndroid5 = uploadMsg;
   Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
   contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
   contentSelectionIntent.setType("*/*");
   if (acceptTypes != null) {
      contentSelectionIntent.putExtra(Intent.EXTRA_MIME_TYPES, acceptTypes);//支持多类型
   }
   Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
   chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
   chooserIntent.putExtra(Intent.EXTRA_TITLE, "文件选择");

   startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
}

第四步:反馈给web

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
   if (requestCode == FILECHOOSER_RESULTCODE) {
      if (null == mUploadMessage)
         return;
      Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
      mUploadMessage.onReceiveValue(result);
      Logger.d("result:" + result);
      mUploadMessage = null;

   } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) {
      if (null == mUploadMessageForAndroid5)
         return;
      Uri result = (intent == null || resultCode != RESULT_OK) ? null : intent.getData();
      if (result != null) {
         mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
         Logger.d("result5:" + result);
      } else {
         mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
      }
      mUploadMessageForAndroid5 = null;
   }
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Selenium本身并不支持直接操作Webview2,但可以通过一些第三方库来实现。以下是使用winrt库来获取Webview2网页部分的示例代码: ```python import winrt.windows.ui.core as core import winrt.windows.ui.input as input import winrt.windows.system as system from selenium import webdriver # 启动Webview2 edge_options = webdriver.EdgeOptions() edge_options.use_chromium = True edge_options.add_argument("disable-gpu") driver = webdriver.Edge(options=edge_options) driver.get("https://www.example.com/") # 获取Webview2控件的句柄 window_handle = driver.current_window_handle app_view = system.DisplayingApplication.get_current().get_app_view_for_current_thread() webview_handle = app_view.get_hosted_view_for_current_thread().core_window.handle core_window = core.CoreWindow.get_for_current_thread() # 模拟键盘按键,使Webview2控件获得焦点 input.Keyboard.get_for_current_view().try_set_input_scope(input.InputScopeNameValue.url) input.Keyboard.get_for_current_view().try_move_focus(input.FocusNavigationDirection.next) # 截取Webview2网页部分的屏幕截图 webview_rect = core_window.bounds webview_bitmap = core.Screenshot.capture(webview_handle, webview_rect) webview_bitmap.save_to_file("webview_screenshot.png") ``` 上述代码,通过winrt库获取了Webview2控件的句柄,然后模拟键盘按键,使Webview2控件获得焦点。最后通过CoreWindow的Screenshot方法截取了Webview2网页部分的屏幕截图。 ### 回答2: 在Selenium获取Webview2网页部分,需要通过以下步骤: 1. 首先,确保已经安装了适当版本的Webview2运行时和驱动程序。 2. 创建一个WebDriver实例,用于控制Webview2浏览器。可以使用selenium的`webdriver.WebView`方法来实现。 3. 使用`get()`方法访问目标网址,例如:`driver.get("https://example.com")`。 4. 要获取Webview2网页部分,可以使用以下方法: - 使用`driver.find_element_by_xpath()`或`driver.find_elements_by_xpath()`方法定位到想要获取的网页元素。 - 使用`driver.execute_script(js_script)`方法执行JavaScript脚本,js_script可以是获取网页元素的JavaScript代码。 - 使用`driver.page_source`属性获取整个网页源代码,然后使用字符串处理方法,如正则表达式或解析器(如BeautifulSoup)来提取所需部分。 5. 如果需要等待特定的网页元素加载完成,可以使用`WebDriverWait`类和`expected_conditions`模块来实现。例如,使用`expected_conditions.presence_of_element_located`等待元素出现。 6. 最后,记得在使用完毕后关闭WebDriver实例,释放资源。可以使用`driver.quit()`方法来关闭浏览器。 需要注意的是,Webview2暂时不支持所有的Selenium功能,可能会有一些限制。在使用Webview2时,建议参考官方文档和相关社区资源来了解更多的用法和解决可能遇到的问题。 ### 回答3: Selenium 是一种用于自动化浏览器操作的工具,它通常用于测试网页应用程序。Selenium 支持许多不同类型的浏览器和窗口,包括普通的网页窗口和 WebView2。 WebView2 是一种用于 Windows 平台的 WebView 技术,它允许嵌入和集成 web 内容到应用程序。要在 WebView2 获取网页的部分内容,可以使用以下步骤: 1. 安装并设置 WebView2 运行环境:首先,你需要在你的计算机上安装 WebView2 运行时。你可以从 Microsoft 的官方网站上下载并安装 WebView2 运行时。安装完成后,设置你的项目依赖库和引用,以便与 WebView2 运行时进行关联。 2. 初始化和加载 WebView2 控件:在你的代码创建一个 WebView2 控件实例,并将其加载到你的应用程序界面。你可以设置 WebView2 控件的位置和大小,以适应你的需求。 3. 获取网页的部分内容:一旦 WebView2 控件加载了网页,你就可以使用 Selenium 的 API 操作 WebView2 网页。利用 Selenium 的元素定位和操作方法,你可以通过在 WebView2 查找和操作网页元素,从而获取网页的部分内容。 例如,你可以使用 Selenium 的 `find_element_ 方法定位指定的 HTML 元素,并使用 `get_attribute` 方法获取该元素的指定属性值。你还可以使用 `text` 方法获取该元素的文本内容。 需要注意的是,由于 WebView2 是用于 Windows 平台的技术,你需要确保 Webview2 控件已正确安装和配置,并正确设置与之关联的项目依赖库和引用。 以上是关于如何使用 Selenium 获取 WebView2 网页部分内容的简要说明。具体的实现方式可能根据你的具体应用场景而有所不同,你可以在 Selenium 和 WebView2 的官方文档找到更详细的信息和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值