【问题标题】:Selenium python click buttonSelenium python点击按钮
【发布时间】:2016-05-08 19:14:18
【问题描述】:

我只是无法单击按钮并转到下一页。到目前为止,我尝试了以下方法

<div class="step__footer">
<button class="step__footer__continue-btn btn " type="submit" name="button">
<span class="btn__content">Continue to payment method</span>
<i class="btn__spinner icon icon--button-spinner"></i>
</button>
<a class="step__footer__previous-link" href="https://checkout.shopify.com/946304/checkouts/1b6e3391268707abb18850300b89e59?step=contact_information">
<svg class="previous-link__icon icon--chevron icon" viewBox="0 0 6.7 11.3" height="11.3" width="6.7" xmlns="http://www.w3.org/2000/svg">
Return to customer information
</a>
</div>

driver = webdriver.Firefox()

driver.implicitly_wait(1) # seconds
driver.find_element_by_css_selector("step__footer__continue-btn btn").click()
driver.implicitly_wait(1) # seconds
driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div[2]/div/form/div[2]/button").click()
driver.implicitly_wait(1) # seconds
driver.find_element_by_name("button").click()
driver.implicitly_wait(1) # seconds
elem.send_keys(Keys.RETURN)
driver.implicitly_wait(1) # seconds
driver.find_element_by_css_selector("div.step__footer>button").click()
driver.implicitly_wait(1) # seconds
driver.find_element_by_css_selector("html.multi-step.mac.firefox.desktop.page--no-banner.page--logo-main.page--show.js.flexbox.flexboxlegacy.rgba.multiplebgs.boxshadow.opacity.cssanimations.csstransitions.generatedcontent.svg.inlinesvg.cors.boxsizing.display-table.pointerevents.placeholder.mediaqueries.floating-labels body div.content div.wrap div.main div.main__content div.step form.edit_checkout.animate-floating-labels div.step__footer button.step__footer__continue-btn.btn").click()
driver.implicitly_wait(1) # seconds
driver.find_element_by_link_text("Continue to payment method").click()
driver.implicitly_wait(1) # seconds
driver.find_elements_by_class_name("step__footer__continue-btn btn ").click()
driver.implicitly_wait(1) # seconds
driver.findElementByXpath("//div[@type='submit'][@name='button']").click();

编辑
关键是重新定位元素,重新加载。由于我点击了一个表单,底层代码发生了变化。 driver.find_element_by_xpath("//button[@type='submit'][@name='button']").click() 因此找不到元素。使用 driver.get ("%s/%s:%s" % (str(sys.argv[4]), str(sys.argv[2]), str(sys.argv[3]))) 重新加载后成功了。

还要查看日志,以防某些格式错误的命令中断流程。

【问题讨论】:

    标签: python html css selenium-webdriver


    【解决方案1】:

    你的选择器有很多错误,试试其中一个

    driver.find_element_by_css_selector(".step__footer__continue-btn.btn").click() # for class with css_selector you need '.' in the beginning
    driver.find_element_by_class_name("step__footer__continue-btn").click() # by_class_name gets only one class
    driver.find_element_by_xpath("//button[@type='submit'][@name='button']").click() # type='submit' and name='button' are <button> tag attributes, not div
    

    您也可以尝试使用显式等待

    WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.class_name, "step__footer__continue-btn"))).click()
    

    编辑

    有时按钮元素区域比可以接收点击的元素更宽。您可以尝试单击“较低”级别的元素

    driver.find_element_by_class_name("btn__content").click() # <span> tag
    # or
    driver.find_element_by_class_name("btn__spinner").click() # <i> tag
    

    【讨论】:

    • 由于某种原因不起作用。它不会跳转到下一页。我尝试了所有方法,包括显式等待。也许页面来源与萤火虫节目不同。我尝试用 print(driver.page_source) 打印它,但由于某种原因我看不到日志
    • 谢谢你。我又试了一次,还是没有反应。我正在尝试打印驱动程序看到的源代码。当脚本在相关步骤停止时,当我手动检查它时,它可能与 FireBug 显示的不同。
    • driver.find_elements_by_class_name("btn__content").click() # tag AttributeError: 'list' object has no attribute 'click' driver.find_elements_by_class_name("btn__spinner").click() # tag AttributeError: 'list' object has no attribute 'click' 我将再次尝试您的原始条目。 print(driver.page_source) 命令可能中断了脚本。
    • @Zsolt 尝试使用更新后的 driver.find_element 方法和显式等待。
    猜你喜欢
    • 2014-02-16
    • 2021-09-14
    • 2018-05-11
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多