Selenium:选择带有子类别的多个下拉列表



试着这样做:

  1. 在"股票代码/股票名称"下输入股票代码,待所有内容都弹出后选择第一项
  2. 在标题类别和文档类型下,选择标题类别"→公告及通告;→新上市(上市发行人/新申请人)->"分配Result">
  3. 点击"搜索按钮"打开新页面
  4. 点击"公告及通告-[分配结果]&;
  5. "下的连结
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox(executable_path = "C:Program FilesMozilla Firefoxgeckodriver.exe")     
driver.implicitly_wait(5)
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml")      
wait = WebDriverWait(driver, 5)              
wait.until(EC.element_to_be_clickable((By.ID, "searchStockCode"))).send_keys("01156)
????

有3到4下拉菜单,在一个下拉菜单中,我们必须向下滚动,我已经在chrome上尝试了下面的代码,似乎工作得很好

示例代码:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.ID, "searchStockCode"))).send_keys("01156")
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='slimScrollDiv']/descendant::tbody/tr[1]"))).click()
#first drop down
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "ALL"))).click()
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Headline Category"))).click()
#Second,3rd, 4th drop down
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "ALL"))).click()
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Announcements and Notices"))).click()
ele = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.droplist-group.droplist-submenu.level2")))
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", ele)
i = 1
for item in driver.find_elements(By.XPATH, "//li[@class='droplist-item droplist-item-level-2']"):
ActionChains(driver).move_to_element(driver.find_element_by_xpath(f"(//li[@class='droplist-item droplist-item-level-2'])[{i}]")).perform()
i = i + 1
if item.text == "New Listings (Listed Issuers/New Applicants)":
item.click()
break
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Allotment Results"))).click()
#Search
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "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
from selenium.webdriver.common.action_chains import ActionChains

我在Chrome中尝试了以下代码,它工作了。

from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path="path")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml")
driver.find_element_by_id("searchStockCode").send_keys("01156")
driver.find_element_by_xpath("//span[text()='CHINANEWENERGY']").click()
driver.find_element_by_id("tier1-select").click()
driver.find_element_by_xpath("//div[contains(@class,'combobox-group')]/div[2]//a[text()='Headline Category']").click()
driver.find_element_by_id("rbAfter2006").click()
driver.find_element_by_link_text("Announcements and Notices").click()
opt = driver.find_elements_by_xpath("//li[@class='droplist-item droplist-item-level-2']")
for o in opt:
driver.execute_script("arguments[0].scrollIntoView(true);",o)
driver.find_element_by_link_text("New Listings (Listed Issuers/New Applicants)").click()
driver.find_element_by_link_text("Allotment Results").click()
driver.find_element_by_link_text("SEARCH").click()
driver.find_element_by_xpath("//a[contains(text(),'ANNOUNCEMENT')]").click()
windows = driver.window_handles
driver.switch_to.window(windows[1])
summary = driver.find_element_by_link_text("Summary")
summary_link = summary.get_attribute("href")
print(summary_link)
summary.click()
time.sleep(2)
driver.quit()

最新更新