如何使用Selenium(使用Java)点击文件附件图标?

huangapple go评论70阅读模式
英文:

How to click on file-attachment icon using selenium (with java)?

问题

我正在创建自动化程序,通过LinkedIn自动发送消息。代码运行正常,但根据我们当前的要求,我希望点击附件按钮,以便在发送文本消息时也可以传递附件。有一些我已经尝试过的要点:

  1. 由于大多数情况下元素ID是动态的,无法获取元素ID。
  2. 不想使用偏移量(x,y),因为一旦有人更改窗口大小,偏移量就会改变。
  3. 无法使用XPath。

然后我尝试了另一个名为“UIVision”的工具,它可以解决该问题,它可以捕获我们想要点击的页面上的特定区域(将其保存在.png文件中),并在运行时点击该区域。因此,我尝试搜索适用于Java的图像比较API,尝试了ASHOT和Sikulix,但都没有奏效。

有人能帮助我吗?

英文:

I am creating automation that automatically sends messages through LinkedIn. The code is working fine, but as per our current requirement, I want to click on the attachment button so as to pass the attachment also with the text message. There are a few points that I have already tried:

  1. Unable to fetch the element ID as its dynamic most of the time.
  2. Don't want to use offsets (x,y) because once anyone changes the size of the window offsets changes.
  3. Unable to use XPath.

Then I checked for another tool "UIVision" which makes a solution to capture that particular area (save it in a .png file) from the page where we want to click and during runtime it clicks there. So I tried searching image comparison API for java, tried with ASHOT and Sikulix too but none worked for me.

Anyone can help me with this?

答案1

得分: 0

当你说你无法使用xpath时,你能否详细说明一下?

这个按钮有一个id - 它是 attachment-trigger-ember1079

如果你担心后面部分是动态的,附件按钮也有一个很好的标题。
你可以尝试使用xpath字符串:

//button[contains(@title,'Attach a file')]

对我来说,在我的领英页面上,这将返回一个对预期的附件对象的唯一命中:
如何使用Selenium(使用Java)点击文件附件图标?

如果这些方法不起作用,还可以使用JavaScript来执行上传操作。
有关处理上传对话框的更多信息(在确定对象后),请查看这里

英文:

When you say your unable to use xpath will you please elaborate?

The button has an id - it's attachment-trigger-ember1079.

If you're concerned about the latter part being dynamic, the attachment button also has a good title.
You can try using xpath string:

//button[contains(@title,'Attach a file')]

For me, on my linked in, this returns a unique hit on the expected attachment object:
如何使用Selenium(使用Java)点击文件附件图标?

If these don't work, there's also the option of using javascript to action the upload.
For more information on handling the upload dialog (after your identify the object) have a look here

huangapple
  • 本文由 发表于 2020年7月23日 20:22:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63054174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定