2016-07-26 177 views
2

我想在python 3網站上找到一個按鈕元素selenium。我嘗試了一些不同的方法,但都失敗了。我用Xpath找到我的元素,但我不知道這是否是更好的方法:硒查找按鈕元素

這是HTML代碼:

<div id="review-buttons-container"> 
<div class="columns"> 
<div class="please-wait" id="review-please-wait" style="display:none;"> 

<span>PROCESSING...</span> 
</div> 
<input id="place_order" type="button" value="Complete Order" class="button end"/> 
</div> 
</div> 

這是我的蟒蛇已經嘗試:

br.find_element_by_xpath("//input[@id='place_order']").click() 

回報:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (606, 678). Other element would receive the click :

  • ...
  • //div[@id='review-buttons-container']/div/input 
    

    回報:

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id='review-buttons-container']/div/input"}

    br.find_element_by_xpath("//form[2]/div[8]/div/input").click() 
    

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//form[2]/div[8]/div/input"}

    任何想法?感謝

    回答

    1

    您可以使用ActionChains點擊它之前移動到元素

    from selenium.webdriver.common.action_chains import ActionChains 
    
    element = br.find_element_by_xpath("//input[@id='place_order']") 
    ActionChains(br).move_to_element(element).perform() # I assume br is your webdriver 
    element.click() 
    

    如果你不想使用xpath可以使用find_element_by_id('place_order')

    你可以找到here更多的方式來定位元素

    0

    喲可以嘗試滾動到這個按鈕,然後點擊使用它的位置和js

    element = driver.find_element_by_id("place_order") 
    element_position = element.location["y"] 
    driver.execute_script("window.scroll(0, {})".format(element_position)) 
    time.sleep(1) #may not be required 
    element.click()