Selenium Java Webdriver: Adding a string to an Xpath

守給你的承諾、 提交于 2019-12-31 06:24:13

问题


I have the folllowing piece of Java in which I want to add a String inside a statement:

     @and ("^I want to change fieldnumber \"([^\"]*)\" , remove whats inside and add the following text: \"([^\"]*)\"$")

    public void testscenario12345(String number,  String text) throws Throwable {
    driver.findElement(By.xpath("//*[contains(@name,'template:view:<INSERT NUMBER STRING HERE>:item:view:1:item:')]")).clear();
    driver.findElement(By.xpath("//*[contains(@name,'template:view:<INSERT NUMBER STRING HERE>:item:view:1:item:')]")).sendKeys(text);

How can I add the number string in the above piece of code? To clarify, If I insert "4" in cucumber, I want the driver to find the element by Xpath, where the @name contains template:view:4:item:view:1:item:


回答1:


What about

driver.findElement(By.xpath("//*[contains(@name,\"template:view:" + number+ ":item:view:1:item:\")]")).clear();

And you probably need the number to be const, that means:

public void testscenario12345(final String number,  String text) throws Throwable {


来源:https://stackoverflow.com/questions/22220036/selenium-java-webdriver-adding-a-string-to-an-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!