<p>有3到4个下拉列表,在其中一个下拉列表中,我们必须向下滚动,我在chrome上尝试了下面的代码,似乎工作正常</p>
<p><strong>示例代码:</strong></p>
<pre><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()
</code></pre>
<p><strong>导入:</strong></p>
<pre><code>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
</code></pre>