【问题标题】:Error when trying to click a button using Selenium尝试使用 Selenium 单击按钮时出错
【发布时间】:2017-06-25 13:08:59
【问题描述】:

我正在使用 Python 上的 Selenium 在 WellGosh.com 上编写“添加到购物车”功能。

我的代码获得了正确的尺寸以添加到购物车并登录我的帐户并到达正确的送货地址,但是当我尝试确认送货方式(联邦快递)时,它不会让我点击继续按钮.

这是一个结帐代码示例:

def Checkout():
#brings you to your cart
driver.get('https://wellgosh.com/checkout/cart')

#clicks to checkout
checkout=driver.find_element_by_xpath('//*[@id="shopping-cart-table"]/tfoot/tr/td/div[2]/a')
checkout.click()

#Log in
login=driver.find_element_by_xpath('//*[@id="login-email"]')
login.send_keys(e_mail)
password=driver.find_element_by_xpath('//*[@id="login-password"]')
password.send_keys(Pass)
LogIn=driver.find_element_by_xpath('//*[@id="checkout-step-login"]/div/div[2]/div/button')
LogIn.click()
cont=driver.find_element_by_xpath('//*[@id="billing-buttons-container"]/button')
cont.click()
driver.implicitly_wait(100)
element = driver.find_element_by_xpath('//*[@id="shipping-method-buttons-container"]/button')
element.click()

我收到此错误:

selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见

这是 HTML 代码的 sn-p:

    </script>
    </div>
    <div class="buttons-set" id="shipping-method-buttons-container">
        <p class="back-link"><a href="#" onclick="checkout.back(); return false;"><i class="fa fa-chevron-left plain"></i>Back</a></p>
        <button type="button" class="button btn-continue" onclick="shippingMethod.save()">Continue</button>
        <span id="shipping-method-please-wait" class="please-wait zoooooom" style="display:none;">
            <i class="fa fa-cog fa-spin plain"></i>
        </span>
    </div>
</form>

【问题讨论】:

  • 那么,如果继续按钮的父元素的 id 是“billing-buttons-container”,那么这个 ID 在你的 HTML sn-p 中的什么位置?

标签: python selenium


【解决方案1】:

如果您在单击按钮时遇到问题,为什么不运行该按钮的“onclick”属性中的 javascript?例如,在您的 HTML sn-p 中,您可以这样做:

driver.execute_script("shippingMethod.save()")

注意,如果此页面使用 iFrame,您必须先将驱动程序切换到适当的 iFrame,然后才能找到/选择其中的子元素。

【讨论】:

  • 直接调用给定的页面 api 方法而不是使用用户界面是一种非常糟糕的做法。
  • 这是真的,奇怪的是它给出了一个元素不可见的异常。这通常意味着可能需要单击其他内容才能使元素可见。是否有任何不在 sn-p 中的父元素显示为“无”?
【解决方案2】:

试试下面的代码,让我知道它是否解决了你的问题:

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

wait(driver, 10).until(visibility_of_element_located((By.XPATH, '//button[text()="Continue"]'))).click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2021-08-30
    相关资源
    最近更新 更多