将 Java 变量添加到 xpath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33196567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Java variable to xpath
提问by Stefan
Is it possible to add a defined Java String variable to an xpath-expression? Here is some code to make it clear:
是否可以将定义的 Java 字符串变量添加到 xpath 表达式?这里有一些代码可以清楚地说明:
String newUser = "NewUser123";
driver.findElement(By.xpath("//td[normalize-space(text())=**newUser**]/a")).click();
The xpath should work with the variable newUser, but I don′t know how.
xpath 应该与变量 newUser 一起工作,但我不知道如何。
回答by Robbie Wareham
Treat the xpath like any other string concatenation or interpolation you would do in Java
像对待在 Java 中所做的任何其他字符串连接或插值一样对待 xpath
回答by Kumrun Nahar Keya
You can try the following code:
您可以尝试以下代码:
String newUser = "NewUser123";
driver.findElement(By.xpath("//td[normalize-space(text())=" + newUser + "]/a")).click();
回答by Jeremiah
In similar situations I will extract the lookup to a constant in the class, and use the String.formatutility to insert the variable(s) where I need it. Note the use of %sin the example constant USER_XPATHbelow.
在类似的情况下,我会将查找提取到类中的常量,并使用String.format实用程序在需要的地方插入变量。请注意以下示例常量USER_XPATH中%s的使用。
I think this is better for readability and maintenance than the inline concatenation option, but both will achieve the goal.
我认为这在可读性和维护方面比内联串联选项更好,但两者都可以实现目标。
/** Format string for the ### xpath lookup.*/
private static final String USER_XPATH ="//td[normalize-space(text())=%s]/a";
Then you can use String.format to insert the variable you want:
然后你可以使用 String.format 插入你想要的变量:
private void myMethod() {
String newUser = "NewUser123";
String fullXpath = String.format(USER_XPATH, newUser);
driver.findElement(By.xpath(fullXpath)).click();
}
回答by Nidhi
Yes, when you choose Java as your programming language while working with selenium, you can perform all things that are logically possible in Java.
是的,当您在使用 selenium 时选择 Java 作为您的编程语言时,您可以执行在 Java 中逻辑上可行的所有事情。
Take your example, xpath is a function which takes string arguments. So Like in Java we concatenate multiple strings using +
. you can do the same thing here.
以您的示例为例,xpath 是一个接受字符串参数的函数。所以就像在 Java 中一样,我们使用+
. 你可以在这里做同样的事情。
String newUser = "NewUser123";
driver.findElement(By.xpath("//td[normalize-space(text())='"+newUser+"']/a")).click();
回答by Garima Thakur
In case if its a number in any loop table it can be written like
如果它是任何循环表中的数字,则可以这样写
String sCol = "1";
字符串 sCol = "1";
driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/article/div/table/tbody/tr["+sRow+"]/td["+sCol+"]")).getText();
driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/article/div/table/tbody/tr["+sRow+"]/td[" +sCol+"]")).getText();
回答by Ashan Mabharana
Xpath
with variable and space
Xpath
有变量和空间
String CasesCount = Utility.WebDriverHelper.Wdriver.findElement
(By.xpath("//button[text()=' "+tabname+"']/span")).getText();
Remove this kind of special braces ()
in text
去掉()
文本中的这种特殊大括号
CasesCount = CasesCount.replaceAll("\(", "").replaceAll("\)","");