如何单击 For Loop 下的搜索按钮(Python Selenium)
How to click on Search button under For Loop (Python Selenium)
我希望我可以 select 下拉菜单中的所有股票,点击“搜索”按钮并点击“导出到 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
点击一个选项,例如(00002) CLP 然后点击 Search 按钮你会得到 for the visibility_of_all_elements_located()
and you can use either of the following :
使用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
你需要诱导 WebDriverWait
() 并等待 element_to_be_clickable
()
点击 Export to CSV
按钮时,页面将刷新,页面将不再可用,您将变得陈旧 exception.To 避免这种情况,您需要重新分配内部元素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))
我希望我可以 select 下拉菜单中的所有股票,点击“搜索”按钮并点击“导出到 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
点击一个选项,例如(00002) CLP 然后点击 Search 按钮你会得到 visibility_of_all_elements_located()
and you can use either of the following
使用
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
你需要诱导 WebDriverWait
() 并等待 element_to_be_clickable
()
点击 Export to CSV
按钮时,页面将刷新,页面将不再可用,您将变得陈旧 exception.To 避免这种情况,您需要重新分配内部元素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))