【问题标题】:How to click the button on the following website?如何点击以下网站上的按钮?
【发布时间】:2019-06-16 06:05:03
【问题描述】:

我是 python 世界的新手,我正在尝试在以下网站上选择几个选项,然后单击搜索按钮以更新结果。但是,我无法让按钮做出响应。

我尝试使用搜索 button.click() 和 .submit() 并尝试隐式等待。我还使用下面的代码等到按钮可点击。执行代码时,它突出显示按钮但似乎没有释放点击;几乎是半点击。

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Safari()
driver.get('https://leasing.com/personal/car-leasing/')
element = driver.find_element_by_id('selUpfront')
select = Select(element)
select.select_by_value("3")
element = driver.find_element_by_id('selMileage')
select = Select(element)
select.select_by_value("8000")
searchbutton = WebDriverWait(driver,     20).until(EC.element_to_be_clickable((By.ID, "search-button")))
searchbutton.click()

我希望搜索结果会根据上述条件进行更新。

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    看来你很接近了。要在元素上click(),您必须诱导 WebDriverWait 以使 元素可点击,您可以使用以下任一解决方案:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#search-button>i.fa.fa-search#search-button-icon"))).click()
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='search-button']/i[@class='fa fa-search' and @id='search-button-icon']"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • 浏览器快照:

    【讨论】:

      【解决方案2】:

      有 2 个带有搜索按钮的元素,您需要使用 xpath 来定位特定的

      searchbutton = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='search-button']")))
      searchbutton.click()
      

      【讨论】:

      • 谢谢,我试过了,但同样的问题发生在点击时它没有做任何事情 - 你可以看到按钮以蓝色突出显示,然后返回橙色,就像它被点击一样,但它不会使用选定的过滤器更新页面。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      相关资源
      最近更新 更多