You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python Selenium实现NCBI Gene页面Gene_ID自动搜索及元素定位

Got it, let's get your NCBI Gene batch query automation up and running with Selenium. I’ve built similar scripts for bulk gene data pulls before, so here’s a practical breakdown of how to handle input field targeting, text entry, and search triggers:

Step 1: Locate the Search Input Field

First, you’ll need to pinpoint the unique attributes of NCBI’s search input box. Here’s how to do it reliably:

  • Open Safari’s Developer Tools (enable it first via Safari > Settings > Advanced > Show Develop menu in menu bar).
  • Navigate to the NCBI Gene page, right-click the search input box, and select Inspect Element.
  • Look for stable attributes like id, name, or placeholder. For NCBI Gene, the input box typically uses id="term" and name="term"—these are your most dependable targets since they rarely change with site updates.

Once you’ve got the element locator, use Selenium’s methods to input the Gene_ID and kick off the search. Here’s a code snippet that integrates with your existing Safari setup:

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

# Your existing Safari launch and navigation code
driver = webdriver.Safari()
driver.get("https://www.ncbi.nlm.nih.gov/gene")

# Example list of Gene_IDs to process (replace with your batch list)
gene_ids = ["1017", "1018", "207", "5728"]

for gene_id in gene_ids:
    try:
        # Wait for the search input to load (avoids race conditions)
        search_input = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, "term"))
        )
        
        # Clear any leftover text and input the current Gene_ID
        search_input.clear()
        search_input.send_keys(gene_id)
        
        # Trigger search: two reliable options
        # Option 1: Press Enter (simplest, works for most cases)
        search_input.send_keys(Keys.RETURN)
        
        # Option 2: Click the search button (use if Enter doesn't trigger the search)
        # search_button = WebDriverWait(driver, 10).until(
        #     EC.element_to_be_clickable((By.ID, "search"))
        # )
        # search_button.click()
        
        # Wait for results page to load (adjust the condition based on what you need to scrape)
        WebDriverWait(driver, 15).until(
            EC.presence_of_element_located((By.CLASS_NAME, "gene-summary"))
        )
        
        # Add your code here to extract data from the results page
        print(f"Successfully processed Gene_ID: {gene_id}")
        
        # Navigate back to the main search page for the next query
        driver.back()
    except Exception as e:
        print(f"Error processing {gene_id}: {str(e)}")
        # Add error handling here (e.g., skip to next ID, take a screenshot for debugging)

# Clean up the browser session
driver.quit()

Key Tips for Robust Automation

  • Always use WebDriverWait: Avoid time.sleep()—waiting for elements to be visible/clickable makes your script resilient to page load delays.
  • Fallback locators: If By.ID stops working (e.g., NCBI updates the site), use alternatives like By.NAME (name="term") or By.XPATH (e.g., //input[@placeholder="Search gene"]).
  • Safari checks: Double-check that "Allow Remote Automation" is enabled in Safari’s Develop menu—you likely have this on since you can launch the browser, but it’s a common hidden pitfall.

内容的提问来源于stack exchange,提问作者Vidyaramanan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:08:58