Selenium: Select multiple dropdowns with subcategory

Viewed 148

Try to do the following:

  1. Input a stock code under "Stock Code/Stock Name", wait till all things pop up and choose the first item
  2. Under Headline Category and Document Type, select "Headline Category" -> "Announcement and Notices" -> "New Listing(Listed Issuers/New Applicants") -> "Allotment Result"
  3. Click "Search Button" to open new page
  4. Click on link under "Announcements and Notices - [Allotment Results]"
        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 Files\Mozilla Firefox\geckodriver.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)

        ????

2 Answers

There are 3 to 4 drop downs and in one of drop down we have to scroll down, I have tried the below code on chrome, seems to be working fine.

Sample code :

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()

Imports :

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

I tried with below code in Chrome, it worked.

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()
Related