【问题标题】:Click on Option in Dropdown List using Python Selenium使用 Python Selenium 在下拉列表中单击选项
【发布时间】:2020-12-14 17:20:47
【问题描述】:

我正在尝试通过 Python-Selenium 更新页面http://stitch.embl.de//cgi/download.pl?UserId=FCCY8Z7drB9z&sessionId=QFV3kq1R2gdD(模拟点击Choose an organism -> Homo sapiens,然后点击Update的动作)

如何执行脚本?

<div style="height:3em;vertical-align:top;"><div id="organism_text_input"><script type="text/javascript">
    function toggleSpeciesFloatingDiv ()
    {
        if(document.getElementById('speciesFloatingDiv').style.visibility != "visible") {
            initiateDropDownSpeciesList();
            document.getElementById('speciesFloatingDiv').style.display = "block";
            document.getElementById('speciesFloatingDiv').style.visibility = "visible";
            document.getElementById('speciesList').focus();
        } else {
            document.getElementById('speciesFloatingDiv').style.display = "none";
            document.getElementById('speciesFloatingDiv').style.visibility = "hidden";
        }
    }
</script>

【问题讨论】:

    标签: javascript python selenium


    【解决方案1】:

    你可以点击选择框->

    box.click()
    

    然后你可以写文本并按回车->

    box.send_keys("Homo Sapiens")
    box.send_keys(Keys.RETURN)
    

    【讨论】:

    • 抱歉,我如何获得box?我尝试使用driver.find_element_by_id("organism_text_input") 获取框元素,但没有成功。
    • @CPak id为“organism_text_input”的div中包含一个id为“species_text”的项目
    • Keys 来自哪里?出现Keys is not defined 错误。
    【解决方案2】:
    from selenium import webdriver
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome()
    delay = 10
    
    driver.get("http://stitch.embl.de//cgi/download.pl?UserId=FCCY8Z7drB9z&sessionId=QFV3kq1R2gdD")
    
    # CLick down arrow on drop down menu
    
    WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '//*[@id="organism_text_input"]/div[1]/div/img'))).click()
    # driver.find_element_by_xpath().click()
    # Now that options are loaded, select "Homo sapiens" from the species list
    select = Select(driver.find_element_by_id('speciesList'))
    select.select_by_visible_text('Homo sapiens')
    
    # Click the 'Select' button in the drop down menu to apply
    driver.find_element_by_class_name('minibutton').click()
    

    【讨论】:

    • 执行您的步骤并重新抓取页面后,我仍然可以看到原始链接。例如,我希望看到9606.protein_chemical.links.v5.0.tsv.gz 。您能否将解决方案扩展几个步骤以获取更新的链接? (抱歉,否则,我无法验证它是否有效)。
    • 可能是网站加载太慢了,我添加了一个明确的等待下拉菜单加载后再继续。无论哪种方式,代码都按我的预期工作。
    • driver.find_element_by_class_name('minibutton').click()之后,如何获取更新页面的HTML? driver.get(...)?
    • 我无法编写您的整个项目。希望我提供的答案符合原问题的要求。
    • 哈哈,好吧。谢谢你的帮助。我已经解决了我自己的问题(见我的回答)。我想为未来的用户扩展您的答案,但没关系。
    【解决方案3】:

    如果我将&amp;species_text=9606 添加到 URL,我可以获得所需的链接 - 最终 URL 变为 http://stitch.embl.de//cgi/download.pl?UserId=FCCY8Z7drB9z&sessionId=QFV3kq1R2gdD&species_text=9606

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 2013-08-05
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      相关资源
      最近更新 更多