首页 > 解决方案 > 无法单击按钮 Shopify/Selenium

问题描述

无法点击 shopify 网站上的“继续付款按钮”。我看过几篇类似的帖子,但大多数都是针对 js 的,并且没有提到错误的微调器部分。

driver.find_element_by_xpath ('//*[@id="continue_button"]/svg')

<div class="content-box__row">
      <div class="radio-wrapper" data-shipping-method="shopify-Standard%20Shipping-15.00">
        <div class="radio__input">
          <input class="input-radio" data-checkout-total-shipping="$15.00" data-checkout-total-shipping-cents="1500" data-checkout-shipping-rate="$15.00" data-checkout-original-shipping-rate="$15.00" data-checkout-total-price="$94.00" data-checkout-total-price-cents="9400" data-checkout-payment-due="$94.00" data-checkout-payment-due-cents="9400" data-checkout-payment-subform="required" data-checkout-subtotal-price="$79.00" data-checkout-subtotal-price-cents="7900" data-checkout-total-taxes="$0.00" data-checkout-total-taxes-cents="0" data-checkout-multiple-shipping-rates-group="false" data-backup="shopify-Standard%20Shipping-15.00" type="radio" value="shopify-Standard%20Shipping-15.00" name="checkout[shipping_rate][id]" id="checkout_shipping_rate_id_shopify-standard20shipping-15_00" />
        </div>
        <label class="radio__label" for="checkout_shipping_rate_id_shopify-standard20shipping-15_00">
          <span class="radio__label__primary" data-shipping-method-label-title="Standard Shipping">
            Standard Shipping


          </span>
          <span class="radio__label__accessory">
            <span class="content-box__emphasis">
              $15.00
            </span>
          </span>
</label>      </div> <!-- /radio-wrapper-->
    </div>

      </div>


      </div> 
    </div> 

  </div>

  <div class="step__footer" data-step-footer>

    <button name="button" type="submit" id="continue_button" class="step__footer__continue-btn btn" aria-busy="false"><span class="btn__content" data-continue-button-content="true">Continue to payment</span><svg class="icon-svg icon-svg--size-18 btn__spinner icon-svg--spinner-button" aria-hidden="true" focusable="false"> <use xlink:href="#spinner-button" /> </svg></button>
  <a class="step__footer__previous-link" href="/18292275/checkouts/38df275516a513f1c08f6c470ef014d0?step=contact_information"><svg focusable="false" aria-hidden="true" class="icon-svg icon-svg--color-accent icon-svg--size-10 previous-link__icon" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><path d="M8 1L7 0 3 4 2 5l1 1 4 4 1-1-4-4"/></svg><span class="step__footer__previous-link-content">Return to information</span></a>
</div>

标签: pythonselenium

解决方案


试试这个 xpath :

//span[text()='Continue to payment']/..

在代码中:

  1. 没有明确的等待

代码 :

driver.find_element_by_xpath("//span[text()='Continue to payment']/..").click()
  1. 使用显式等待

代码 :

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Continue to payment']/.."))).click()

进口:

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

更新 1:

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(driver.find_element_by_xpath("//span[text()='Continue to payment']/..")).click().perform()

更新 2:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(50)
driver.get('https://seelbachs.com/products/sagamore-spirit-cask-strength-rye-whiskey')
wait = WebDriverWait(driver, 50)

frame_xpath = '/html/body/div[5]/div/div/div/div/iframe'
wait = WebDriverWait(driver, 10)
# wait until iframe appears and select iframe
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, frame_xpath)))

# select button
xpath = '//*[@id="enter"]'
time.sleep(2)
element= driver.find_element_by_xpath(xpath)
ActionChains(driver).move_to_element(element).click(element).perform()

# go back to main page
driver.get('https://seelbachs.com/products/sagamore-spirit-cask-strength-rye-whiskey')

# add to cart
atc= driver.find_element_by_xpath('//button[@class="btn product-form__cart-submit product-form__cart-submit--small"]')
atc.click()

# check out
co= driver.find_element_by_xpath ('//*[@id="shopify-section-cart-template"]/div/form/footer/div/div[2]/input[2]')
co.click()

# enter email
driver.find_element_by_xpath('//*[@id="checkout_email"]').send_keys('no@yahoo.com')
time.sleep(1)
# enter first name
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_first_name"]').send_keys('John')
time.sleep(1)
# enter last name
driver.find_element_by_xpath('//*[@id="checkout_shipping_address_last_name"]').send_keys('Smith')
time.sleep(1)
# enter address
driver.find_element_by_xpath ('//*[@id="checkout_shipping_address_address1"]').send_keys('111 South Street')

# enter city
driver.find_element_by_xpath ('//*[@id="checkout_shipping_address_city"]').send_keys('Cocoa')

# enter zip
driver.find_element_by_xpath ('//*[@id="checkout_shipping_address_zip"]').send_keys('263153')

# enter phone
driver.find_element_by_xpath ('//*[@id="checkout_shipping_address_phone"]').send_keys('5555555'+ u'\ue007')

select = Select(wait.until(EC.visibility_of_element_located((By.ID, "checkout_shipping_address_province"))))
select.select_by_value('UK')

wait.until(EC.element_to_be_clickable((By.ID, "continue_button"))).click()

推荐阅读