如何使用 Selenium 和 Python 单击启用 ember.js 的按钮

How to click on the ember.js enabled button using Selenium and Python

我一直在努力让它可以点击,但我就是不明白我做错了什么。 我也在尝试诱导webdriverwait,这样一出现就被点击了。

到目前为止,这是我的代码:

def order(k):
    driver = webdriver.Chrome(os.getcwd()+"\webdriver\chromedriver.exe") 
    driver.get("website.com/login-to-checkout")
    driver.find_element_by_id('i0116').send_keys(k["email"])
    driver.find_element_by_id('i0118').send_keys(k["password"])
    driver.find_element_by_id('idSIButton9').click()
    delay()
    #sign in button
    driver.find_element_by_id('idSIButton9').click()
    #Button below I cant get to be clicked
    with webdriver.Chrome() as driver:
        wait = WebDriverWait(driver, 7)
        wait.until(presence_of_element_located((By.CSS_SELECTOR, "#ember1053")))
        driver.find_element(By.id, "ember1053").click()

这是我试图使其可点击的按钮的源代码:

<div id="ember1037" class="btn-group m-b-lg m-t-lg order-call-to-action ember-view"><!---->        <!--biBehavior 80 means place order Place Order-->

<button aria-live="polite" type="button" tabindex="0" data-m="{&quot;aN&quot;:&quot;shoppingCart&quot;,&quot;cN&quot;:&quot;PlaceOrder&quot;,&quot;bhvr&quot;:80}" id="ember1053" class="btn theme-default btn-primary cli-purchase ember-view"><!---->            Place order

</button></div>

这可能会有所帮助,但我在使用 id 查找按钮时遇到了 webdriver 没有点击按钮的问题。 我发现的解决方法是使用 xpath 而不是 id。 像这样,值得一试。

driver.find_element_by_xpath("""//*[@id="submit-button"]""").click()

所需的元素是 Ember.js element and the value of the id attribute of the <button> will keep changing dynamically, every time you access the AUT(Application Under Test). Hence to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following :

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.theme-default.btn-primary.cli-purchase.ember-view[id^='ember'][type='button'][aria-live='polite']"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn theme-default btn-primary cli-purchase ember-view' and starts-with(@id,'ember')][contains(., 'Place order') and @aria-live='polite']"))).click()
    
  • 注意:您必须添加以下导入:

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

参考资料

您可以在以下位置找到一些相关的详细讨论:

  • Selenium - Finding element based on ember