MMarie
MMarie

Reputation: 35

Locating a button by xpath or id

The website source is:

<iframe id="fast-checkout-iframe">
<div  class="celwidget" >
        <span class="a" data-action="place-order">
           <span id="checkout-place-order-button" >
             <span class="a-button-inner">
               <input id="fast-checkout-button" type="submit" value="Place your order" >
                 <span id="fast-checkout-place-order-button-announce" >Place your order
</span></span></span></span></div>
</iframe>

I'm trying to click to switch to the iframe and click the button.

self.driver.switch_to.frame(self.driver.find_element_by_tag_name("iframe"))
self.driver.find_element_by_id("fast-checkout-button").click()

But I get:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="fast-checkout-button"]"}

What should I do? Thanks!

Upvotes: 0

Views: 651

Answers (2)

KunduK
KunduK

Reputation: 33384

Use WebDriverWait() and wait for element_to_be_clickable() and following xpath.

wait = WebDriverWait(driver, 20)
buttons = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='fast-checkout-button']")))
buttons.click()

You need to import below libraries

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

If you still getting timeout error then please check it is under iframe or not


Update.

you need to switch to iframe first in order to access the element inside iframe.

#Switch to frame
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "fast-checkout-iframe")))  
#Click on the button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, //input[@id='fast-checkout-button']"))).click()

#To switch back to the main
driver.switch_to.default_content()

Upvotes: 1

Breaks Software
Breaks Software

Reputation: 1761

As @KunduK mentions, it would be best to wait for your elements. In addition, just looking at the source, I think that you should be attempting to click on the input element, not the span. so use the id fast-checkout-button instead.

Upvotes: 0

Related Questions