1

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>
3
  • 1
    What is your goal, the DOM, and the error you are receiving? Aug 30, 2019 at 16:26
  • paste the html snippet of the webelement that you are trying to find.
    – Alok
    Aug 31, 2019 at 14:56
  • <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> Aug 31, 2019 at 15:19

1 Answer 1

1

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']"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.