首页 > 解决方案 > 如何在 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"]

标签: pythonselenium-webdriverwebdriverjupyter-notebookwebdriverwait

解决方案


您可以改为执行更高效的 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)

推荐阅读