【问题标题】:How to click on Search button under For Loop (Python Selenium)如何单击 For Loop (Python Selenium) 下的搜索按钮
【发布时间】:2020-08-01 02:04:48
【问题描述】:

我希望我可以在下拉菜单中选择所有股票,点击“搜索”按钮并点击“导出为 CSV”按钮。但是,我被卡在选择选项后无法点击“搜索”按钮。请指教。谢谢

   from selenium import webdriver
   from selenium.webdriver.support.ui import Select, WebDriverWait
   from selenium.webdriver.common.by import By
   from selenium.webdriver.support import expected_conditions as EC
   from time import ctime

   browser = webdriver.Chrome()
   browser.get("https://www.hkex.com.hk/eng/sorc/options/statistics_hv_iv.aspx")

   select = Select(WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,        
    "//select[@id='underlying']"))))


   browser.implicitly_wait(10)
   optionsList = []

   #iterate over the options, place attribute value in list
   for option in select.options:
       option.click()
       optionsList.append(option.get_attribute("value"))

       # below code is not able to work properly but this is only what I am hoping to work
       elem = browser.find_element_by_link_text("Search")
       elem.click()
       elem1 = driver.find_element_by_link_text("Export to CSV")
       elem1.click()
       print(elem)

   print(optionsList)
   print(len(optionsList))


   browser.quit

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    你需要诱导WebDriverWait()并等待element_to_be_clickable()

    单击Export to CSV 按钮时,页面将刷新,页面将不再可用,您将收到过时异常。为避免这种情况,您需要重新分配 for 循环内的元素。

    代码

    from selenium import webdriver
    from selenium.webdriver.support.ui import Select, WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    browser = webdriver.Chrome()
    browser.get("https://www.hkex.com.hk/eng/sorc/options/statistics_hv_iv.aspx")
    browser.maximize_window()
    select = Select(WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,"//select[@id='underlying']"))))
    optionsList = []
    items=select.options
    for i in range(len(items)):
        #re-assigned select elements again to avoid stale exception
        select = Select(WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='underlying']"))))
        items = select.options
        print(items[i].get_attribute("value"))
        select.select_by_value(items[i].get_attribute("value"))
        optionsList.append(items[i].get_attribute("value"))
        #click on search button
        WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//span[@class='floatleft']//a[@title='Search']"))).click()
        #click on download button
        WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Export to CSV']"))).click()
    
    print(optionsList)
    print(len(optionsList))
    

    【讨论】:

      【解决方案2】:

      点击一个选项,例如(00002) CLP 然后点击 Search 按钮,您可以将WebDriverWait 诱导为visibility_of_all_elements_located(),您可以使用以下任一Locator Strategies

      • 使用CSS_SELECTOR

        driver.get("https://www.hkex.com.hk/eng/sorc/options/statistics_hv_iv.aspx")
        Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#underlying")))).select_by_value("00002")
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td a[title='Search']"))).click()
        
      • 使用XPATH

        driver.get("https://www.hkex.com.hk/eng/sorc/options/statistics_hv_iv.aspx")
        Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='underlying']")))).select_by_value("00002")
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td//a[@title='Search']"))).click()
        
      • 浏览器快照:

      • 注意:您必须添加以下导入:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 2018-11-04
        • 1970-01-01
        • 1970-01-01
        • 2021-03-18
        • 1970-01-01
        相关资源
        最近更新 更多