python - 如何在 Jupyter 中通过 Selenium 和 Python 单击文本为 Find What County I'm In 的按钮
问题描述
这是我想点击的按钮的html:我想我应该抓住可见的那个。(我猜)
<form id="search-form">
<div class="input-group hidden-xs">
<span class="input-group-btn">
<button class="btn btn-default" type="button" onclick="getUserExactLocation();scrollToMap();">Find What County I'm In</button>
</span>
<input type="text" class="form-control" placeholder="Enter address here" id="address">
<span class="input-group-btn">
<button class="btn btn-default" type="button" onclick="findAddress();scrollToMap();">Find County for Address</button>
</span>
</div>
<div class="input-group visible-xs">
<input type="text" class="form-control" placeholder="Enter address here" id="address-xs">
<button class="btn btn-default xs-btn" type="button" onclick="findAddress();scrollToMap();">Find County for Address</button>
<button class="btn btn-default xs-btn" type="button" onclick="getUserExactLocation();scrollToMap();">Find What County I'm In</button>
</div>
</form>
我可以把数据放到搜索区。但是,我无法提交我的按钮以获取返回数据:我有以下代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup as bs
from requests_futures.sessions import FuturesSession
from urllib.parse import urljoin
from collections import namedtuple
driver=webdriver.Chrome()
driver.get("https://www.mapdevelopers.com/what-county-am-i-in.php")
mainsoup = bs(driver.page_source, 'lxml')
listB=[]
listC= ['1209 SE 2nd Ave,Grand Rapids,MN,55744', '415 9th Avenue,Granite Falls,MN,56241', '22 East 6th St,Mantorville,MN,55955']
for query in listC:
address= driver.find_element_by_id("address")
address.send_keys(query)
submitButton=driver.find_element_by_css_selector("button[onclick*='getUserExactLocation();scrollToMap();']").click()
county_result= driver.find_element_by_id('display_county')
listB = listB + [county_result.text]
print(listB)
对于 submitButton,我也尝试:
submitButton= driver.find_element_by_css_selector('button.btnbtn-defaultxs-btn').click()
和
driver.find_element_by_xpath("//button[@class='btn btn-default xs-btn'][@type='button'][contains(., 'input-group visible-xs')]").click()
结果应该是这样的:
["Yellow Medicine County", "Yellow Medicine County", "Dodge County"]
解决方案
您可以改为执行更高效的 xhr 发布请求并解析 json 响应。
import requests
url = 'https://www.mapdevelopers.com/data.php?operation=geocode'
headers = {
'User-Agent': 'Mozilla/5.0',
'Accept': 'application/json, text/javascript, */*; q=0.01'}
listB=[]
listC= ['1209 SE 2nd Ave,Grand Rapids,MN,55744', '415 9th Avenue,Granite Falls,MN,56241'] #, '22 East 6th St,Mantorville,MN,55955']
for query in listC:
body = {'address' : query}
res = requests.post(url, data = body).json()
listB.append(res['data']['county'])
print(listB)
推荐阅读
- c# - 在 C# Net Core API 调用中使用 html 和 Hightchart 创建报告
- android - Android espresso 测试失败,并显示“无法解析活动:Intent { (has extras) }”
- mongodb - Robo 3T 无法通过 SSL 连接 MongoDB 副本集
- indexing - 将索引列添加到报告(不是表)中
- c++ - 从 Java 接口到 C++ 代码的转换
- active-directory - 企业根 CA 和从属 CA 在同一台服务器上
- python - 查找 y = 0 的 x 值以进行扩展回归
- java - 显示 JDK 模块的依赖关系
- python - 创建一个以中心为中心的星号矩形
- r - ggplot2 geom_density_2d 允许按因子填充,但不能按值填充