1

I am trying to automate a payment using selenium java via a modal. I am able to enter the CC details however when I exit the iframe and try to click the pay button I get the following error message

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 89.0.4389.23 (61b08ee2c50024bab004e48d2b1b083cdbdac579-refs/branch-heads/4389@{#294}) on port 11203
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Mar 19, 2021 3:35:12 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
7
cc details entered
default content
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="chakra-modal--body-630"]/button"}
  (Session info: chrome=89.0.4389.90)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'Joseffs-MacBook-Pro.local', ip: 'fe80:0:0:0:cd7:6e5b:a729:ae56%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.16', java.version: '1.8.0_281'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 89.0.4389.90, chrome: {chromedriverVersion: 89.0.4389.23 (61b08ee2c5002..., userDataDir: /var/folders/8h/ply4smss33q...}, goog:chromeOptions: {debuggerAddress: localhost:64606}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: ebf1ef088c26a2c2d70561235a14f215
*** Element info: {Using=xpath, value=//*[@id="chakra-modal--body-630"]/button}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at GsMarket.Market.main(Market.java:67)

My code is

Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.default_content_setting_values.notifications", 2);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", prefs);
    WebDriverManager.chromedriver().setup();
    ChromeDriver driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.get(baseURL);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.findElement(By.xpath("//button[contains(text(),'Log in')]")).click();
    driver.findElementById("user_login").sendKeys(userName);
    driver.findElement(By.cssSelector("input[type='password']")).sendKeys(password);
    
    // submit
    driver.findElement(By.cssSelector("button[type='submit']")).click();

    // Shop Now Button
    driver.findElement(By.xpath("//*[contains(text(),'Shop Now')]")).click();


    driver.findElement(By.xpath("//*[contains(text(),'2,000')]")).click();
    int size = driver.findElements(By.tagName("iframe")).size();
    System.out.println(size);

//      // enter CC details
    driver.switchTo().frame(4);
    

driver.findElement(By.cssSelector("input[name='cardnumber']")).sendKeys(CCNUM);
        driver.findElement(By.cssSelector("input[name='exp-date']")).sendKeys(expiry);
        driver.findElement(By.cssSelector("input[name='cvc']")).sendKeys(cvc);
        driver.findElement(By.cssSelector("input[name='postal']")).sendKeys(zip);
    System.out.println("cc details entered");
    driver.switchTo().defaultContent();
    System.out.println("default content");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    // Click purchase button
    driver.findElement(By.xpath("//*[@id=\"chakra-modal--body-630\"]/button")).sendKeys(Keys.RETURN);;

    Thread.sleep(2000);
    driver.close();
1
  • Short the error trace.It looks wast on Android:-) – Gaj Julije Mar 19 at 18:41
1

Use wait() method properly so that the desired element is visible/present.

Your code seems okay. You can recheck the xpath of button. Use click() method instead of sendKeys(Keys.RETURN);

 // Click purchase button
    driver.findElement(By.xpath("//*[@id=\"chakra-modal--body-630\"]/button")).click();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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