1

I receive this error whenever I try to click on the Shopify credit card text field.

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"number"} (Session info: chrome=ip)

My Code:

    import java.awt.im.InputContext;
    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import static org.hamcrest.CoreMatchers.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.Select;

    public class AccountTest {

    private WebDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {

        System.setProperty("webdriver.chrome.driver", "/Applications/chromedriver");
        driver = new ChromeDriver();

        driver.get("https://www.google.com");


        baseUrl = "https://shopnicekicks.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testUntitledTestCase() throws Exception {
        driver.get("https://shopnicekicks.com/products/nice-kicks-wrist-lanyard-black");
        System.out.println("Product Found");
        driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Quantity'])[1]/following::input[2]")).click();
        System.out.println("Added to Cart");
        driver.findElement(By.id("tos_acceptance")).click();
        driver.findElement(By.name("checkout")).click();
        System.out.println("Checking Out");
        //driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Phone'])[1]/following::span[1]")).click();
        driver.findElement(By.id("checkout_email")).click();
        driver.findElement(By.id("checkout_email")).sendKeys("abc@gmail.com");
        driver.findElement(By.id("checkout_buyer_accepts_marketing")).click();
        driver.findElement(By.id("checkout_shipping_address_first_name")).sendKeys("John");
        driver.findElement(By.id("checkout_shipping_address_last_name")).sendKeys("Doe");
        driver.findElement(By.id("checkout_shipping_address_address1")).sendKeys("123 Sesame Street");
        driver.findElement(By.id("checkout_shipping_address_city")).sendKeys("Test");
        new Select(driver.findElement(By.id("checkout_shipping_address_country"))).selectByVisibleText("United States");
        driver.findElement(By.id("checkout_shipping_address_province")).click();
        new Select(driver.findElement(By.id("checkout_shipping_address_province"))).selectByVisibleText("Georgia");
        driver.findElement(By.id("checkout_shipping_address_zip")).sendKeys("12345");
        driver.findElement(By.id("checkout_shipping_address_phone")).sendKeys("1234567891");
        driver.findElement(By.name("button")).click();
        driver.findElement(By.name("checkout[shipping_rate][id]")).click();
        driver.findElement(By.name("button")).click();
        Thread.sleep(5000);
        System.out.println("Done Sleeping...");
        driver.findElement(By.id("number")).click();
        driver.findElement(By.id("number")).clear();       driver.findElement(By.id("number")).sendKeys(String.valueOf(1234123412341234l));
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}

The Specific error occurs here.

driver.findElement(By.id("number")).click();
    driver.findElement(By.id("number")).clear();
         driver.findElement(By.id("number")).sendKeys(String.valueOf(1234123412341234l));

URL: Shopnicekicks.com

closed as off-topic by c32hedge, Bharat Mane, IAmMilinPatel, Nitin Rastogi, NarendraC Feb 8 at 5:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers." – c32hedge, Bharat Mane, IAmMilinPatel, Nitin Rastogi, NarendraC
If this question can be reworded to fit the rules in the help center, please edit the question.

  • It's clear that it is unable to find the element as it displays NoSuchElementException. Did you try using Xpath, ClassName, Selectors etc? – PassionateCoder Feb 6 at 18:47
  • Verify the locator, if it is present inside a frame and sync issues. With said that, could you share the html dom and the exact area of affected code; that would make things easier. – Kshetra Mohan Prusty Feb 7 at 6:20
0

The credit card number text field lies in an Iframe. See the screenshot below:

enter image description here

Solution:

You have to switch to the iframe before interacting with any element inside it.

driver.switchTo().frame("id_of_the_iframe"); // You can also use name or index of the iframe element

And yes, don't forget to switch back to the main or parent frame after done with the iframe.

driver.switchTo().defaultContent();

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