繁体   English   中英

Selenium-python单击按钮始终返回错误

[英]selenium-python clicking a button always returns an error

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

我可以使用elem = driver.find_element(by='xpath', value="//div[@id='gwt-debug-search-button']")选择一个元素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>

我从未使用过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()

因此,存在一些错误,一个错误是除非绝对需要,否则不要使用动作链。 但是另一个是您实际上没有将任何东西传递给click事件。

它应该是:

chains.click(get_ideas).perform()

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

您应该只使用硒的功能来进行常规的单击和操作,而我只会使用动作链来进行双击和其他类似的行为,这实际上是无论如何都要做的。

这样做,mabe02的答案就更容易了,也更容易阅读!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM