【问题标题】:How to Upload the File using Selenium to Div tag. Which tag is not having input as type如何使用 Selenium 将文件上传到 Div 标签。哪个标签没有输入作为类型
【发布时间】:2019-05-22 20:36:48
【问题描述】:

我正在尝试将文件上传到 div 标签。它没有类型作为文件输入。所以请建议使用文件路径的 sendkey 的人请检查我的完整问题。

我有这样的标签。

Screenshot of the tag

我已经尝试使用 sendkeys 查找元素并从本地上传文件。这不适合我。

这是我尝试过的。

GlobalVariables.chromeDriver_Main.findElement(By.xpath("//*[@id=\"card-uploader\"]"))
                    .sendKeys("C:\\Users\\Dhaval Bhimajiyani\\Documents\\Lightshot\\Screenshot_207.png");

这给了我这样的错误。

org.openqa.selenium.WebDriverException: unknown error: cannot focus element

我需要了解如何将文件上传到此 div 标签。哪个没有输入类型作为文件。

感谢您提前回复。

【问题讨论】:

  • 请阅读为什么a screenshot of code is a bad idea。粘贴代码并正确格式化。
  • 如果您可以提供minimal reproducible example,也许我们可以提供帮助。但就你的问题而言,这是一个猜谜游戏。
  • 您需要提供您的标记或相关网站的链接以获得帮助。 Selenium 上传功能仅适用于 <input type="file">

标签: java selenium selenium-webdriver automation


【解决方案1】:

试试这个。

# get the button element
ele = driver.find_element_by_id("card-uploader")
# add a hidden file input ( might have to change the onchange event based on the events associated to the button in above line as you don't have a form)
driver.execute_script("var x=  document.createElement('INPUT');x.setAttribute('type', 'file'); x.setAttribute('onchange','this.form.submit()');x.setAttribute('hidden', 'true'); arguments[0].appendChild(x);",ele)
# send the file path here ( this should upload the file)
driver.find_element_by_xpath("//input[@type='file']").send_keys("C:\\Users\\Dhaval Bhimajiyani\\Documents\\Lightshot\\Screenshot_207.png")

【讨论】:

  • 我没有输入文件。请考虑进去
  • 上述代码会在第4行添加一个输入字段(隐藏),在第6行完成上传。
  • 顺便说一句,我可以测试这段代码,因为我没有找到示例屏幕。
  • 试图完成这项工作。如果那不锻炼,我会尽快给你示例屏幕。谢谢。
  • 酷!是否可以共享示例屏幕,以便我可以从头开始对其进行测试并进行任何必要的更改/改进。
猜你喜欢
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 2017-04-29
  • 2015-10-21
  • 1970-01-01
  • 2021-08-17
  • 2020-06-30
  • 1970-01-01
相关资源
最近更新 更多