我正在尝试使用 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"> </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/