'Can't click button with selenium

I'm a newcomer when it comes to javascript and selenium. I have created a simple add to cart project, but the one i am currently working on im having some troubles. The HTML code is:

<div class="buttons-set" id="shipping-method-buttons-container">
<button type="button" class="dark" onclick="shippingMethod.save()" onkeypress="shippingMethod.save()">Continue</button>
<span id="shipping-method-please-wait" class="please-wait icon icon--notch" style="display:none;">
Loading next step... </span>
</div>

I can't seem to figure out anyway where i can click the Continue button. I have tried things such as

driver.findElement(By.linkText("Continue")).click(); 
driver.findElement(By.xpath(//*[@id='shipping-method-buttons-container']/button)).click(); 

and many other combinations but none seemed to work.



Solution 1:[1]

Try getting the element by its class name:

driver.find_element_by_class_name("dark").click(); 

Solution 2:[2]

I believe you have used implicit wait. If not we need to add it.

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

Also try this below xpath.

driver.findElement(By.xpath("//button[contains(text(),'Continue']")).click(); 

Hope this helps. Thanks.

Solution 3:[3]

Try this below code using cssSelector locator.

driver.findElement(By.cssSelector("button[class='dark'][type='button']")).click();

OR

Try to click the button using java-script executor.

WebElement continue_button = driver.findElement(By.cssSelector("button[class='dark'][type='button']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", continue_button);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 santhosh kumar
Solution 3 Jainish Kapadia