javascript - selenium-python 单击按钮总是返回错误

标签 javascript python selenium gwt

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

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

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

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()

关于javascript - selenium-python 单击按钮总是返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35407061/

相关文章:

javascript - 使用 npm 终端的 npm 脚本奇怪行为

javascript - http header 的断言

javascript - Angular 2如何将带有参数的函数传递给子组件?

python - 一些字符串没有在 django i18n 模块中翻译

python - 计算相同字符的最大子串

linux - 在 redhat linux 上安装 Selenium 服务器

selenium - 我可以用不同的端口启动一个selenium服务器吗

javascript - 在 Grails 中引用 CSS 文件和 JS 文件 - twitter-bootstrap

python - 如何使用 python 更新、旋转和显示 ply 文件?

python - 通过从列表中获取元素来更改 xpath