简体   繁体   English

如何为在XPath中传递的char A到Z提供循环,这可能是我们不需要多次编写XPath

[英]How to provide loop for char A to Z which is passed in XPath after that may be we don't need to write XPath for multiple times

Are there any solutions for XPath? XPath是否有解决方案?
I want to traverse from char ' A ' to ' Z ', but the problem is which is indide the XPath... 我想从字符“A”“Z”穿越,但问题是,这是对球铁QT500 ...的XPath

So I think we solve this by loop, but I do not know how to provide s loop for that... For code optimization we do not need to write XPath for multiple times.. 因此,我认为我们可以通过循环来解决此问题,但是我不知道如何为此提供s循环。对于代码优化,我们不需要多次编写XPath。

I have tried it by loop, but it doesn't work. 我已经尝试过循环,但它不起作用。

WebElement A = driver.findElement(By.xpath("//a[text()='**A**']"));
act.click(A).perform();
char c = 'A';
for (c='A';c<='Z';c++)
{
     String s = Character.toString(c);          
}

Use String formatter - define a "template" with the fixed structure of the string, and the format specifier. 使用字符串格式化程序 -使用字符串的固定结构和格式说明符定义“模板”。
Presuming you want to replace the "A" in the "**A**" text: 假设你要替换"A""**A**"的文字:

String templ = "//a[text()='**%s**']";
for (char c='A'; c<='Z'; c++){
     WebElement A = driver.findElement(By.xpath(String.format(templ, c)));
     /* if you want to see the constructed string/locator:
     System.out.println(String.format(templ, c)); */
     act.click(A).perform();
}

Make the xpath selector dynamic, this is sample logic, you can improve later, try this: 使xpath选择器动态,这是示例逻辑,您可以稍后进行改进,请尝试以下操作:

String[] letters = {"A","B","C"....};
Actions action = new Actions(driver);

for (int i=0; i<letters.length(); i++)
{
    WebElement element = driver.findElement(By.xpath("//a[text()=\"**" + letters[i] + "**\"]"));
    action.moveToElement(element).click().perform();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM