[英]How to write path to webelement using Xpath for JavascriptExecutor (Selenium)
Trying to write autotest on Selenium WebDriver. 尝试在Selenium WebDriver上编写自动测试。 Having on page Webelement with CSS locator:
在页面上使用CSS定位器实现Webelement:
@FindBy (css = "#FeedbackMessage")
protected WebElement fm;
Also have following working method of focusing out of this element: 也有以下集中于此元素的工作方法:
public void focusout(){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].blur();", fm);
The question is how to do same but with xpath locator usage. 问题是如何使用xpath定位器使用相同的方法。 The xpath of an element is
//textarea[@id='FeedbackMessage']
. 元素的xpath是
//textarea[@id='FeedbackMessage']
。
Trivial usage 琐碎的用法
@FindBy (xpath = "//textarea[@id='FeedbackMessage']")
protected WebElement fm;
is NOT working 不管用
Actually found an answer by debugging in IDEA. 通过在IDEA中调试实际上找到了答案。
@FindBy (xpath = "//textarea[@id='FeedbackMessage'][1]") protected WebElement fm;
The reason - xpath locator //textarea[@id='FeedbackMessage'] can provide list of elements (even if i have only one element). 原因-xpath定位器// textarea [@ id ='FeedbackMessage']可以提供元素列表(即使我只有一个元素)。 but usage of CSS or ID selector always takes first found element.
但是使用CSS或ID选择器始终要先找到第一个元素。
So I just needed to indicate index of element 所以我只需要指出元素的索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.