【发布时间】:2016-10-23 21:05:48
【问题描述】:
我正在尝试使用 Python 和 Selenium 开发一个程序,以自动执行在给定半径内查找人口的过程。我选择使用一个带有半径和地址的网站。我可以输入半径和地址,但是,我需要能够单击由谷歌地图提供支持的下拉菜单中的选项,以便以正确的方式格式化地址,以便谷歌找到地址和地点地图上的半径。
我遇到的问题是下拉菜单是由浏览器生成的,我无法检查元素。我不确定如何使用 Selenium 使用 Python 或 Javascript 访问元素,以便单击第一个下拉菜单选项。单击下拉菜单选项后,半径将在地图上生成,然后我将能够继续单击计算按钮以查找半径内的人口。
TLDR:如何在 Selenium 中使用 Python 或 Javascript 点击由谷歌地图提供支持的下拉菜单?无法检查下拉菜单。
网址:https://www.freemaptools.com/find-population.htm
Python 代码:
def putInputs(driver,address,radius):
print "Entering inputs:"
radius_input = "document.getElementById('radiusinputmi').value = " + radius
driver.execute_script(radius_input)
driver.find_element_by_id("radiusinputmi").send_keys(radius)
driver.find_element_by_id("tb_searchlocation").send_keys(address)
# i need to click on the drop down menu so the radius shows up!
更新:
我发现下拉菜单显示在 HTML 页面的底部。但是,我仍然不确定如何选择它。我有以下 Javascript 代码可以更接近选择所需的元素。
Javascript:
// address of location to find
var address = "Indiana University Blooington";
// get input text box
var location_input = document.getElementById("tb_searchlocation");
// set input text box to the address given
location_input.value = address;
// get the drop down menu with the available options given input location
var x = document.getElementsByClassName("pac-container pac-logo")[1];
// make the google maps options drop down visible
x.setAttribute("style","width: 212px; position: absolute; left: 2px; top: 901px;");
// get the first option from the google maps drop down menu
var items = x.getElementsByClassName("pac-item");
// HOW CAN I SELECT THE FIRST DROP DOWN MENU??
// tried:
// items[0].focus();
// items[0].select();
// items[0].click();
【问题讨论】:
标签: javascript python selenium