2016-02-15 74 views
0

我想點擊一個按鈕,使用python-selenium綁定;迄今爲止沒有任何運氣嘗試過各種選擇器。我正在使用Chromedriver。selenium-python點擊一個按鈕總是返回一個錯誤

我可以選擇使用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> 

回答

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

所以有一些錯誤,一個是不使用行動鏈,除非絕對必要的。但另一個是你實際上沒有傳遞任何東西給你的點擊事件。

它應該是:

chains.click(get_ideas).perform() 

與做動作鏈的真正問題,是它緩存的事件。這意味着如果你不想重新命名你曾經做過的每一個新動作,你必須在每次重新使用之前清除你的動作鏈或者在每次新的使用中重命名鏈。

你應該只是使用硒的力量來定期點擊和事物,我只會使用動作鏈來進行雙擊和其他這樣的行爲,這實際上是它打算做的反正。

這樣做,mabe02的答案是更容易,更容易閱讀!