How to improve element selector?



  • How can I improve this selector?

    WebElement ele2 = driver.findElement(By.xpath(
    "//*[@id=\"nav-flyout-shopAll\"]/div[3]/div[1]/div[1]/div/a[4]/span[1]"
    ));
    

    Thanks

    package SelenuimSession;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    
    /**
     * 
     * @author ASHUTOSH SINGH
     *
     */
    
    public class AmazonClick {
       public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.setProperty("webdriver.chrome.driver", 
          "C:/chromedriver_win32/chromedriver.exe");
       WebDriver driver = new ChromeDriver();
       driver.manage().window().maximize();
       driver.get("https://www.amazon.in/");
    
       WebElement ele = driver.findElement(By.id("nav-link-shopall"));
       WebElement ele1 = driver.findElement(By.xpath("//*[@id=\"nav-flyout-
                                   shopAll\"]/div[2]/span[1]/span"));
       performClick(ele,ele1,driver);
    }
    
    /**
     * The below method was created for general purpose utility to pass the params as well as          
     *  the driver 
     * @param ele
     * @param ele1
     * @param driver
     */
    public static void performClick(WebElement ele,WebElement ele1,WebDriver driver) {
        Actions act=new Actions(driver);
        act.moveToElement(ele).perform();
        act.moveToElement(ele1).perform();
    
        WebElement ele2 = driver.findElement(By.xpath("//*[@id=\"nav-flyout-
                             shopAll\"]/div[3]/div[1]/div[1]/div/a[4]/span[1]"));
        ele2.click();
        }   
    }
    

    DOM:

    <div id="nav-flyout-shopAll" class="nav-catFlyout nav-flyout" style="display: none; position: absolute; top: 57px; left: 192px;">
        <div class="nav-arrow" style="position: absolute; left: 74.6719px;">
            <div class="nav-arrow-inner"></div>
        </div>
    <div class="nav-template nav-flyout-content nav-tpl-itemList">
        <span class="nav-hasPanel nav-item" data-nav-panelkey="EchoPanel" role="navigation" aria-label="Echo &amp; Alexa">
            <span class="nav-text">Echo &amp; Alexa</span>
    


  • Assuming that you want to get the element "Echo Spot" from the Amazon.in page.

    WebElement ele2 = driver.findElement(driver.FindElement(By.XPath("//* 
    [text()='Echo Spot']"));
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2