【问题标题】:Selenium find button elementSelenium 查找按钮元素
【发布时间】:2016-11-30 01:48:41
【问题描述】:

我想在 selenium on python 3 的网站上找到一个按钮元素。我尝试了一些不同的方法,但都失败了。我使用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>

这是我已经在 python 上尝试过的:

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

返回:

selenium.common.exceptions.WebDriverException:消息:未知错误:元素在点(606、678)不可点击。其他元素会收到点击:

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

    返回:

    selenium.common.exceptions.NoSuchElementException:消息:没有这样的 元素:无法定位元素: {"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:消息:没有这样的 元素:无法定位元素: {"method":"xpath","selector":"//form[2]/div[8]/div/input"}

    有什么想法吗?谢谢

    【问题讨论】:

      标签: python html python-3.x selenium selenium-webdriver


      【解决方案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更多定位元素的方法

      【讨论】:

        【解决方案2】:

        你可以尝试使用它的位置和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()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-13
          • 2022-01-19
          • 2023-01-22
          • 2021-03-05
          • 2018-10-12
          • 2023-01-03
          • 2021-08-12
          相关资源
          最近更新 更多