Javascript 使用Python Selenium单击下拉列表中的选项
我正试图通过Python Selenium更新页面(模拟点击Javascript 使用Python Selenium单击下拉列表中的选项,javascript,python,selenium,Javascript,Python,Selenium,我正试图通过Python Selenium更新页面(模拟点击选择一种生物体->智人,然后点击更新) 如何执行脚本 <div style="height:3em;vertical-align:top;"><div id="organism_text_input"><script type="text/javascript"> function toggleSpeciesFloatingDiv (
选择一种生物体->智人,然后点击更新)
如何执行脚本
<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>
函数切换SpeciesFloatingDiv()
{
if(document.getElementById('speciesFloatingDiv').style.visibility!=“visible”){
initiateDropDownSpeciesList();
document.getElementById('speciesFloatingDiv').style.display=“block”;
document.getElementById('speciesFloatingDiv').style.visibility=“visible”;
document.getElementById('speciesList').focus();
}否则{
document.getElementById('speciesFloatingDiv').style.display=“无”;
document.getElementById('speciesFloatingDiv').style.visibility=“hidden”;
}
}
您可以单击选择器框->
box.click()
然后您可以编写文本并按enter->
box.send_keys("Homo Sapiens")
box.send_keys(Keys.RETURN)
如果我将&species\u text=9606
添加到URL,我可以获得所需的链接-最终的URL将变为对不起,我如何获得框
?我尝试使用驱动程序获取box元素。按id(“有机体文本输入”)查找元素,但无效。@CPak在id为“有机体文本输入”的div中包含一个id为“物种文本”的项键从何而来?未定义获取键的错误。执行步骤并重新刷新页面后,我仍然可以看到原始链接。例如,我希望看到9606.protein\u chemical.links.v5.0.tsv.gz
。您能否将您的解决方案扩展几个步骤以获得更新的链接?(很抱歉,否则,我无法验证它是否工作)。可能网站加载太慢,我添加了一个显式等待下拉菜单加载后再继续。无论哪种方式,代码都按我的预期工作。在驱动程序之后。通过\u class\u name('minibutton')查找\u元素。单击()
,如何获取更新页面的HTML<代码>驱动程序。获取(…)
?我无法编写您的整个项目。希望我提供的答案符合原始问题的要求。哈哈,好的。谢谢你的帮助。我已经解决了自己的问题(见我的答案)。我想为未来的用户详细介绍你的答案,但没关系。
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()