简体   繁体   中英

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.

Try getting the element by its class name:

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

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM