【问题标题】:I want to find the Locator type with locator content in selenium我想在 selenium 中找到具有定位器内容的定位器类型
【发布时间】:2016-04-04 21:14:29
【问题描述】:

我想在 selenium 中找到带有定位器内容的定位器类型。以下是我编写的通过传递定位器内容来获取定位器类型的函数。

当我执行Verify.java 时,它将转到Function.java 并从那里转到Element.java 查找元素定位器类型并返回函数,在函数中我将执行必要的操作,如发送键或单击。

在Verify.java 中,我给出了文本框和按钮的 Xpath。我的意图是去检查我传递的定位器内容是否属于哪个定位器。

它通过检查第一个 if 本身而不去catch 块并且不移动到else if 来验证其他定位器类型而停止。如果我从第一个 if 到 Xpath 评论它是否工作。它不是循环和检查。

谁能给我建议解决方案?

(Testcase) Verify.Java
----------------------
package cm;
import org.testng.annotations.Test;
public class Verify extends Function{
    @Test
    public void Check(){
        Browser("Chrome", "https://www.google.co.in");
        Enter("//*[@id='lst-ib']", "Karthick");
        Click(".//*[@id='sblsbb']");
    }
}


Function.Java
-------------
package cm;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class Function extends Element{
    public void Enter(String LocatorContent, String Value) {
        FindElement(LocatorContent).sendKeys(Value);
    }
    public void Click(String LocatorContent) {
        FindElement(LocatorContent).click();
    }
}


Element.Java
------------
package cm;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class Element extends Browser {
    public Element()
    {
        this.driver = driver;
    }

    public WebElement FindElement(String LocatorContent){
        //this.driver = driver;
        WebElement elemen = null;

        if (driver.findElement(By.id(LocatorContent)).isDisplayed()) {
            try {
                elemen = driver.findElement(By.id(LocatorContent));
                System.out.println("element locator is id");
            } catch (Exception e) {
                System.out.println("Element Not Found given with Locator Id : "+LocatorContent);
            }
            return elemen;
        }
        else if (driver.findElement(By.name(LocatorContent)).isDisplayed()) {
            try {
                elemen = driver.findElement(By.name(LocatorContent));
            } catch (Exception e) {
                System.out.println("Element Not Found given with Locator Name : "+LocatorContent);
            }
            return elemen;
        }
        else if (driver.findElement(By.className(LocatorContent)).isDisplayed()) {
            try {
                elemen = driver.findElement(By.className(LocatorContent));
            } catch (Exception e) {
                System.out.println("Element Not Found given with Locator ClassName : "+LocatorContent);
            }
            return elemen;
        }
        else if (driver.findElement(By.cssSelector(LocatorContent)).isDisplayed()) {
            try {
                elemen = driver.findElement(By.cssSelector(LocatorContent));
            } catch (Exception e) {
                System.out.println("Element Not Found given with Locator Css : "+LocatorContent);
            }
            return elemen;
        }
        else if (driver.findElement(By.xpath(LocatorContent)).isDisplayed()) {
            try {
                elemen = driver.findElement(By.xpath(LocatorContent));
                System.out.println("It Found the element "+LocatorContent);
            } catch (Exception e) {
                System.out.println("Element Not Found given with Locator Xpath : "+LocatorContent);
            }
            return elemen;
        }
        else if (driver.findElement(By.linkText(LocatorContent)).isDisplayed()) {
            try {
                elemen = driver.findElement(By.linkText(LocatorContent));
            } catch (Exception e) {
                System.out.println("Element Not Found given with Locator LinkText : "+LocatorContent);
            }
            return elemen;
        }
        else if(driver.findElement(By.partialLinkText(LocatorContent)).isDisplayed()) {
            {
                try {
                    elemen = driver.findElement(By.partialLinkText(LocatorContent));
                } catch (Exception e) {
                    System.out.println("Element Not Found given with Locator PartialLinkText : "+LocatorContent);
                }
                return elemen;
            }
        }
        return elemen;
    }
}

【问题讨论】:

    标签: selenium-webdriver


    【解决方案1】:

    请在下面找到更新的代码,希望这会起作用。

    public WebElement FindElement(String LocatorContent) throws Exception{
        this.driver = driver;
        WebElement elemen = null;
        try { 
            if (driver.findElement(By.id(LocatorContent)).isDisplayed()) { 
                elemen = driver.findElement(By.id(LocatorContent));                
                System.out.println("element locator is id"); 
                return elemen;
            }
        }
        catch(Exception ex){ }
        try {    
            if (driver.findElement(By.name(LocatorContent)).isDisplayed()) { 
                elemen = driver.findElement(By.name(LocatorContent)); 
                System.out.println("element locator is name"); 
                return elemen;
            }
        }
        catch(Exception ex){ }
        try {   
            if (driver.findElement(By.className(LocatorContent)).isDisplayed()) { 
                elemen = driver.findElement(By.className(LocatorContent)); 
                System.out.println("element locator is className"); 
                return elemen;
            }
        }
        catch(Exception ex){ }
        try {  
            if (driver.findElement(By.cssSelector(LocatorContent)).isDisplayed()) { 
                elemen = driver.findElement(By.cssSelector(LocatorContent)); 
                System.out.println("element locator is cssSelector"); 
                return elemen;
            }
        }
        catch(Exception ex){ }
        try {
            if (driver.findElement(By.linkText(LocatorContent)).isDisplayed()) { 
                elemen = driver.findElement(By.linkText(LocatorContent)); 
                System.out.println("element locator is linkText"); 
                return elemen;
            }
        }
        catch(Exception ex){ }
        try {
            if (driver.findElement(By.partialLinkText(LocatorContent)).isDisplayed()) { 
                elemen = driver.findElement(By.partialLinkText(LocatorContent)); 
                System.out.println("element locator is partialLinkText"); 
                return elemen;
            }
        }
        catch(Exception ex){ }
        try {
            if (driver.findElement(By.xpath(LocatorContent)).isDisplayed()) { 
                elemen = driver.findElement(By.xpath(LocatorContent)); 
                System.out.println("element locator is xpath");
                return elemen;
            }
        } 
        catch (Exception e) { }      
        if (elemen.equals(null))
            throw new Exception("Please not found");
        return elemen;
    }
    

    【讨论】:

      【解决方案2】:

      当元素未找到和代码中断时,它不会循环作为第一个(以下)条件生成异常,因为您没有处理它。

      if (driver.findElement(By.id(LocatorContent)).isDisplayed())  
      

      您应该通过检查字符串(定位器)来创建函数,如下所示:

      public WebElement ByLocator(String locator) {
          By result = null;
          if (locator.startsWith("//")) {
              result = By.xpath(locator);
          } else if (locator.startsWith("css=")) {
              result = By.cssSelector(locator.replace("css=", ""));
          } else if (locator.startsWith("id=")) {
              result = By.id(locator.replace("id=", ""));
          } else{
             result = By.name(locator);
          }
          return driver.findElement(result );
      } 
      

      希望这对你有用。

      【讨论】:

      • 嗨萨迪克,我不会给出“id=''”或“css=”“”。我只会传递代码中提到的值。那么我可以使用这个代码吗.
      • 在您的代码中,它会增加执行时间,因为它会按顺序查找元素,假设如果您正在传递 linkText,那么它将检查先前的条件,这将需要一些时间。所以愚蠢的方法是你在字符串(定位器)中提到一些标志,并在识别定位器类型后应用查找元素函数
      • 如果您使用相同的代码,那么您应该在 try catch 博客中添加条件代码,例如:try { if (driver.findElement(By.id(LocatorContent)).isDisplayed()) { elemen = driver.findElement(By.id(LocatorContent)); System.out.println("元素定位器为id"); } } catch (Exception e) { System.out.println("未找到元素,带有定位器 ID:"+LocatorContent); } 返回元素;
      • 它与您提供的上述代码工作正常,但它仅用于 id 我如何将它用于各种定位器,如 xpath、css、linktext..in 尝试。你能告诉我吗
      • 以上代码适用于 css、name、xpath 和 id,为链接文本等添加更多条件
      【解决方案3】:
      Latest edited
      -------------    
      package cm;
          import org.openqa.selenium.By;
          import org.openqa.selenium.WebElement;
          public class Ele extends Browser {
      
              public WebElement FindElement(String LocatorContent) throws Exception{
                  this.driver = driver;
                  WebElement elemen = null;
                  try { 
                      if (driver.findElement(By.id(LocatorContent)).isDisplayed()) { 
                          elemen = driver.findElement(By.id(LocatorContent)); 
                          System.out.println("element locator is id"); 
                      }
                      else if (driver.findElement(By.name(LocatorContent)).isDisplayed()) { 
                          elemen = driver.findElement(By.name(LocatorContent)); 
                          System.out.println("element locator is name"); 
                      }
                      else if (driver.findElement(By.className(LocatorContent)).isDisplayed()) { 
                          elemen = driver.findElement(By.className(LocatorContent)); 
                          System.out.println("element locator is className"); 
                      }
                      else if (driver.findElement(By.cssSelector(LocatorContent)).isDisplayed()) { 
                          elemen = driver.findElement(By.cssSelector(LocatorContent)); 
                          System.out.println("element locator is cssSelector"); 
                      }
                      else if (driver.findElement(By.linkText(LocatorContent)).isDisplayed()) { 
                          elemen = driver.findElement(By.linkText(LocatorContent)); 
                          System.out.println("element locator is linkText"); 
                      }
                      else if (driver.findElement(By.partialLinkText(LocatorContent)).isDisplayed()) { 
                          elemen = driver.findElement(By.partialLinkText(LocatorContent)); 
                          System.out.println("element locator is partialLinkText"); 
                      }
                      else if (driver.findElement(By.xpath(LocatorContent)).isDisplayed()) { 
                          elemen = driver.findElement(By.xpath(LocatorContent)); 
                          System.out.println("element locator is xpath"); 
                      }
                  } 
                  catch (Exception e) { 
                       if (driver.findElement(By.xpath(LocatorContent)).isDisplayed()) { 
                          elemen = driver.findElement(By.xpath(LocatorContent)); 
                          System.out.println("Element locator is xpath"); 
                      }
                      //System.out.println("Element Not Found given with Locator : "+LocatorContent); 
                  }
                  return elemen;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2021-11-13
        • 1970-01-01
        • 2018-04-04
        • 2011-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 1970-01-01
        相关资源
        最近更新 更多