使用参数通过名称查找硒中的定位器

时间:2019-02-17 21:06:47

标签: java selenium

我正在努力寻找如何通过其名称查找元素,而不是通过乘以元素定位符,而是为其类型创建一个并传递参数的方法。例如,有一个页面,其中有10个按钮“添加到购物车”以显示不同的项目(“笔记本电脑A”,“笔记本电脑b”,“笔记本电脑c”等),因此,我不想创建3个不同的元素,而是想要一个,例如> > webElement elementByName(String itemName) = driver.findElement(By.xpath("//div[@button='add to cart' and @title = '" + itemName+ "']")) 硒中是否存在类似的东西?我是硒新手,找不到类似的东西。谢谢

2 个答案:

答案 0 :(得分:0)

是的,有可能。在分享“答案”之前,我想指出以下几点:

一些注意事项

您需要提供的xpath对Selenium而言并不特殊。它是W3C标准。 https://www.w3.org/TR/2017/REC-xpath-datamodel-31-20170321/

如果您使用的是Chrome,那么甚至在尝试在Selenium中使用它之前,您都可以先通过访问开发人员工具(F12)并按“ CTRL + F”并在搜索中输入xpath来测试xpath出现的方框。

如果您的xpath与某个元素匹配,则它将以黄色突出显示。如果该xpath有效,则将相同的xpath传递到Selenium中也应该起作用,除非有其他原因(例如使用框架等)。例如,Shadow DOM xpath并不是特别友好。

按可见文本搜索

Laptop A; Laptop B; Laptop C为例,如果要基于可见文本查找元素,则可以使用text()函数(xpath提供)。

driver.findElement(By.xpath("//div[text()='Laptop A']");   
driver.findElement(By.xpath("//div[text()='Laptop B']");

考虑到上述情况,因此可以将字符串AB放入数组中,并根据需要遍历它们:

for(int i=0; i<arr.size; i++){
    driver.findElement(By.xpath("//div[text()='Laptop " + arr[i] + "']");   
}

此外,您还可以使用contains函数,其作用类似于正则表达式:

driver.findElement(By.xpath("//div[contains(.,'Laptop')]");

这将匹配任何具有Laptop可见文本的内容,即Laptop ALaptop bThis is a Laptop等。

编辑:基于以下评论

public WebElement selectButtonByName(String name){
     return driver.findElement(By.xpath("//div[@button='add to cart' and @title = '" + name+ "']"));
}

然后使用它:

WebElement tvElement = selectButtonByName("TV").click();

答案 1 :(得分:0)

如果我正确理解,则需要包装findElement方法并为其创建自定义方法,最佳实践是始终包装并为所有与驱动程序相关的操作提供自定义方法,这将长期帮助您,并且可扩展的。这是一个过于简化的示例:

第一步是包装驱动程序:

public class MyDriver {
    //initialise your webdriver, the way you see fit and lets assume you name it 
      myDriver;

    // create your custom find element method
    public WebElement findElement(final By by){
      return myDriver.findElement(by);
    }

    // create a method which uses the findElemet method and passes the webelement to next 
    // method
    public WebElement click(final By bys){
      return clickElement(findElement(bys);
    }

    // this is the click method, which you can use directly for webelements
    public WebElement clickElement(final WebElement element){
      Actions action = new Action(myDriver);
      action.moveToElement(element).click(element).build().perform();
      return element;
    }
}


// Now that we have our basic wrapper, lets use it for your needs:

public class YourTestCase {
    MyDriver driver = new MyDriver(); //this is not best way to do it, just an example

    // now you can use whatever parameter you like and add it with custom xpath logic
    private WebElement myElementFinder(final String parameter){
      return driver.findElement(By.xpath("//div[@button='add to cart'][@title = '" +  
        parameter + "']"));
    }

    // here you click the button, by passing the passing only
    public void clickButton(final String buttonText){
      driver.click(myElementFinder(buttonText))
    }
}

通过采用这种逻辑,您可以构建一个可靠的框架,希望它能为您提供帮助,在需要更多说明时发表评论,欢呼。