简体   繁体   中英

Selenium is unable to find a specific button to click

I am using Selenium on Python to automate my checkout process on a website. The problem is I am unable to click the "Continue To Order Review" button and have tried multiple approaches. The error I keep receiving is "Message: stale element reference: element is not attached to the page document" .

I tried to use explicit wait and it did not work:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[@data-attr='continueToOrderReviewBtn']"))).click()


I also tried to find the button with text that it contains:

try:
    element = driver.find_element_by_xpath("//button[contains(text(),'Continue To Order Review')]")
except NoSuchElementException:
    print("Button not found")
action = ActionChains(driver)
action.move_to_element(element).click().perform()

The buttons div tag is as follows: 在此处输入图像描述

<div class="ncss-col-sm-12 pb5-sm prl5-sm va-sm-t ta-sm-r">
   <button data-attr="continueToOrderReviewBtn" class="ncss-brand pt2-sm pr5-sm pb2-sm pl5-sm ncss-btn-accent continueOrderReviewBtn mod-button-width ncss-brand
                    pt3-sm prl5-sm pb3-sm pt2-lg pb2-lg d-sm-b d-md-ib u-uppercase u-rounded fs14-sm">Continue To Order Review</button>
</div>

The html is not wrapped in an iFrame. I also tried to search for the buttons respective xpath but the script still can't seem to find and click it. I am still new to using Selenium and learning something new about it everyday but it seems that I can't get around this issue.

Due to DOM operation, some element may be temporary inaccessible or it is loaded more than one time. It cause the stale element reference which you receive.

Solution for this problem is to load element more than one time:

for i in range(0, 10):
    try:
        element = driver.find_element_by_xpath("//button[contains(text(),'Continue To Order Review')]")
    except NoSuchElementException:
        print("Button not found")

three ways to click on an element in selenium webdriver

from selenium.webdriver.common.keys import Keys

driver.get('https://www.examplesitenike.com')
# dret = ngRet({},ddriver,doptions) 

element=driver.find_element(By.XPATH, '//*[@id="gen-nav-commerce-header-v2"]/div[3]/div[1]/div/div/div[3]/div/button')


modalwindow=driver.find_element(By.XPATH, '//*[@class="pre-modal-view"]')


if not modalwindow.is_displayed():
    try:element.click()
    except:pass

if not modalwindow.is_displayed():
    try:driver.execute_script("arguments[0].click();", element)
    except:pass

if not modalwindow.is_displayed():
    element.sendKeys(Keys.RETURN)

inputemail=driver.find_element(By.XPATH, '//input[@type="email"]')
if inputemail.is_displayed():
    inputemail.send_keys('mylogin')
    

inputemail=driver.find_element(By.XPATH, '//input[@type="password"]')
if inputemail.is_displayed():
    inputemail.send_keys('mypassword')

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