2016-07-26 180 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()