如何使用Selenium WebDriver访问隐藏文件上传字段 [英] How to access hidden file upload field with Selenium WebDriver

查看:108
本文介绍了如何使用Selenium WebDriver访问隐藏文件上传字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的HTML:

upload_input = $driver.find_element(:id, "//input[@name = 'coupon_file']")
$driver.execute_script "$('input').show();"
upload_input.send_keys file

我正在尝试上传文件,但收到错误消息:

I'm trying to upload a file but get the error:

没有这样的元素:无法找到元素

no such element: Unable to locate element

推荐答案

upload_input = $ driver.find_element(:id,"//input [@name ='coupon_file']")

upload_input = $driver.find_element(:id, "//input[@name = 'coupon_file']")

实际上您做错了. 您正在尝试使用xpath语法查找上载元素,但提到了id定位符不正确.它应该如下所示:-

Actually you're doing incorrect. You're trying to locate upload element using xpath syntax but mentioned id locator which is incorrect. It should be as below :-

upload_input = $driver.find_element(:xpath, "//input[@name = 'coupon_file']")
upload_input.send_keys file

或者您也可以使用name定位器轻松找到该上传元素:-

Or you can easily locate this upload element using name locator instead as :-

upload_input = $driver.find_element(:name, "coupon_file")
upload_input.send_keys file

这篇关于如何使用Selenium WebDriver访问隐藏文件上传字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆