【问题标题】:selenium-python clicking a button always returns an errorselenium-python 点击​​按钮总是返回错误
【发布时间】:2016-02-16 00:12:52
【问题描述】:

我正在尝试使用 python-selenium 绑定单击按钮;到目前为止,尝试了各种选择器,但没有任何运气。我正在使用 Chromedriver。

我可以使用elem = driver.find_element(by='xpath', value="//div[@id='gwt-debug-search-button']") 选择一个元素没有错误,但是当尝试点击它时显示element is not visible

我使用了动作链,它通过没有任何错误但没有点击按钮。我无法弄清楚问题所在。如果您以前解决过类似问题,请分享。

get_ideas = driver.find_element(by='xpath', value="//span[@id='gwt-debug-search-button-content'][normalize-space()='Get ideas']")
chains = ActionChains(driver)
chains.click(on_element=elem).perform()

这里是html源代码:

<div tabindex="0" class="goog-button-base goog-inline-block goog-button aw-btn aw-larger-button aw-save-button" role="button" id="gwt-debug-search-button">
    <input type="text" tabindex="-1" role="presentation" style="opacity: 0; height: 1px; width: 1px; z-index: -1; overflow: hidden; position: absolute;">
        <div class="goog-button-base-outer-box goog-inline-block">
            <div class="goog-button-base-inner-box goog-inline-block">
                <div class="goog-button-base-pos">
                    <div class="goog-button-base-top-shadow">&nbsp;</div>
                    <div class="goog-button-base-content">
                        <span id="gwt-debug-search-button-content">Get ideas</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

【问题讨论】:

    标签: javascript python selenium gwt


    【解决方案1】:

    我从未使用过ActionChains,而是更喜欢这种方法:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait #set wait time
    from selenium.webdriver.support import expected_conditions as EC #specify the expected condition
    
    driver = webdriver.Firefox() # Or whatever you prefer
    driver.get(your_website_url)
    WebDriverWait(driver, 30).until(EC.title_contains(my_website_title))
    

    在您的情况下,您可能希望让驱动程序等待您的页面包含您要单击的按钮。

    关于点击按钮,我建议你使用类似的东西:

    # option 1
    get_ideas = driver.find_element_by_xpath("//span[@id='gwt-debug-search-button-content']")
    # option 2
    get_ideas = driver.find_element_by_link_text("Get ideas")
    
    get_ideas.click()
    

    【讨论】:

      【解决方案2】:

      所以有一些错误,一个是除非绝对需要,否则不要使用动作链。但另一个是你实际上并没有将任何东西传递给你的点击事件。

      应该是:

      chains.click(get_ideas).perform()
      

      做动作链的真正问题是缓存事件。这意味着如果您不想重命名您将要执行的每个新操作,您必须在每次重用之前清除您的操作链,或者在每次新使用时重命名链。

      您应该只使用 selenium 的功能进行常规点击和其他操作,我只会使用操作链进行双击和其他此类行为,这实际上是它本来打算做的。

      这样做,mabe02 的答案要容易得多,也更容易阅读!

      【讨论】:

        猜你喜欢
        • 2014-02-16
        • 2016-05-08
        • 2021-09-14
        • 2018-05-11
        • 1970-01-01
        • 2016-04-29
        • 2021-11-06
        相关资源
        最近更新 更多