简体   繁体   English

如何使用Xpath for JavascriptExecutor(Selenium)向Webelement写入路径

[英]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.

相关问题 如何不使用javascriptExecutor在Selenium Webdriver中突出显示Webelement - How to highlight webelement in selenium webdriver not using javascriptExecutor 使用Firefox 35的JavascriptExecutor无法在Selenium中突出显示WebElement - Unable to highlight WebElement in Selenium using JavascriptExecutor for Firefox 35 使用 JavascriptExecutor 的 Selenium Datepicker - Selenium Datepicker using JavascriptExecutor webelement selenium - 如何使用 xpath 找到元素并使用 onclick 链接 - webelement selenium - how to found an element with xpath and link with onclick 如何通过使用Selenium JavaScriptExecutor获取HTML5缓存状态 - How to get HTML5 Cache status by using Selenium JavaScriptExecutor 如何在selenium中使用JavascriptExecutor右键单击svg元素 - How to right click on a svg element using JavascriptExecutor in selenium 如何使用 Selenium 在 Oracle OTBI 中定位 WebElement - How to locate WebElement in Oracle OTBI using Selenium 使用Selenium中的JavascriptExecutor类捕获控制台错误 - Capture console error using JavascriptExecutor class in selenium 使用JavascriptExecutor在Selenium中生成JavaScript警报 - generating javascript alert in selenium using JavascriptExecutor 使用JavascriptExecutor selenium创建一个链接为背景的图像 - create an image with link as background using JavascriptExecutor selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM