Home > database >  Selenium no such element exists
Selenium no such element exists

Time:06-01

I am trying to perform a simple checkout flow on my staging website, but I cant seem to find the element. I tried to use selenium IDE which works but when it comes to coding in java I keep getting stuck on secure checkout

this is the element button I want to click

<a  href="javascript:void(0);" onclick="onCheckout()" data-stepid="cartstep04">
<img src="https://release.squareoffnow.com/public/assets/images/checkout/svg/secure.svg"  alt="">Secure Checkout</a>

This is the code i have written so far

package googleTestCases;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class simpleCartFlow {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","/Users/manavmehta/Desktop/squareoffSeleniumProjects/chromedriver");
        WebDriver driver=new ChromeDriver();
        driver.get("http://release.squareoffnow.com/");
        driver.manage().window().setSize(new Dimension(1440, 789));
        driver.findElement(By.linkText("Products")).click();
        driver.findElement(By.cssSelector(".store-buy-pro-button")).click();
        driver.findElement(By.cssSelector(".pro-twinpack-button")).click();
        driver.findElement(By.cssSelector(".whole-purchase-button")).click();
        driver.findElement(By.cssSelector(".productAvailability > .click-button")).click();
        driver.findElement(By.cssSelector(".giftpackSubmit")).click();
        System.out.println("button not clicked");
        driver.findElement(By.cssSelector(".checkout-anchor")).click();
        System.out.println("button clicked");


        driver.quit();
    }
}

instead of using a CSS selector I also tried to use linkText but it still didn't work

I keep getting this error enter image description here

CodePudding user response:

I suspect your issue is due to not waiting long enough for the element to appear. I ran the following successfully using the exact same selectors you had (although with Python instead of Java):

from seleniumbase import BaseCase

class MyTestClass(BaseCase):
    def test_base(self):
        self.open("https://release.squareoffnow.com/")
        self.click_link("Products")
        self.click(".store-buy-pro-button")
        self.click(".pro-twinpack-button")
        self.click(".whole-purchase-button")
        self.click(".productAvailability > .click-button")
        self.click(".giftpackSubmit")
        self.click(".checkout-anchor")

Full disclosure: This particular framework, SeleniumBase is one that I personally built, and it uses smart-waiting to make sure that elements have fully loaded before taking action. Java probably has something similar so that you can wait for the element to be clickable so that you don't have to sleep for an arbitrary amount of time between steps.

  • Related