0

I am learning selenium web driver and practicing Amazon site. I found a difficulty to automate the country selection in Amazon site. Kindly help me what locators methods to be used to select the country on the pop up. amazon URL.

Switch to Alert is not helping me..!

0
0

For the above scenario you could below xpath or css locator mentioned and then select the value you want to.

Xpath:

//a[@id='icp-touch-link-country']

CSS:

a#icp-touch-link-country
0
  • You can learn locators from these tutorials,

    1.SeleniumEasy

    2.ToolsQA

    3.Guru99

    4.QTPSelenium

  • You can use Firefox browser add-ons like Firebug, Firepath, FireFinder and gradually you will be able to locate an element by writing Xpaths on your own.

  • In Chrome browser, you can right-click on the desired web element and inspect. The HTML property belonging to that web element would be highlighted in the developer tool. Right click > Copy > Copy Xpath
2
  • I guess instead of using tools it would be better if we could program an xpath. Using firebug like tools or copy method won't necessarily give us a desired and reliable xpath or element locator. Feb 21 '18 at 5:42
  • Yes, I accept. But initially, we can use these for learning and then we will be able to write it on our own Feb 21 '18 at 5:47
0

Refer the below mentioned script and here I have used "className" locator for selecting the country on the popup.

       public static void main(String[] args) 
{       
        System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.amazon.com/");

        //Scrolls down
        ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,5000)");
        //Clicks the country popup
        driver.findElement(By.xpath("//span[text()='United States']")).click();

        //Switches control to the new window
        Set<String> eachwin = driver.getWindowHandles();
        for (String win : eachwin) {

            driver.switchTo().window(win);

            //session id of the current window
            System.out.println(win);
        }

        //Selecting value from country dropdown
        WebElement ele = driver.findElement(By.className("a-native-dropdown"));
        Select dropdown = new Select(ele);
        dropdown.selectByVisibleText("India");

        driver.findElement(By.xpath("(//input[@class='a-button-input'])[2]")).click();

        //Switches to the new window with country selected
        Set<String> eachwin2 = driver.getWindowHandles();
        for(String win2 : eachwin2)
        {
            driver.switchTo().window(win2);

            //session id of the current window
            System.out.println(win2);
        }

    }

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