有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java按名称查找selenium中带有参数的定位器

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


共 (2) 个答案

  1. # 1 楼答案

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

    一些需要注意的事情

    您需要提供的xpath对Selenium来说没有什么特别的。这是W3C标准。 https://www.w3.org/TR/2017/REC-xpath-datamodel-31-20170321/

    如果您使用的是Chrome,甚至在尝试在Selenium中使用它之前,您可以先通过转到Developer Tools(F12)并按“CTRL+F”,然后在出现的搜索框中键入xpath来测试xpath

    如果xpath与元素匹配,它将以黄色突出显示。如果该xpath有效,那么如果您将相同的xpath传递到Selenium中,它也应该有效,除非有其他原因,例如使用框架等。例如,影子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();
    
  2. # 2 楼答案

    如果我理解正确,您需要使用findElement方法包装您的应用程序,并为其创建一个自定义方法,最佳做法是始终使用wrap,并为所有与驱动程序相关的操作提供自定义方法,这将从长远来看对您有所帮助,并且具有可扩展性。下面是一个过于简单的例子:

    第一步是包装你的司机:

    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))
        }
    }
    

    通过采用这种逻辑,你可以建立一个坚实的框架,希望它有帮助,如果需要更多解释,请发表评论,干杯