如何通过 Selenium 和 Jupyter 中的 Python 单击文本为“查找我所在的县”的按钮

How to click on the button with text as Find What County I'm In through Selenium and Python in Jupyter

这是我想要点击的按钮的 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>

我可以把数据放到搜索区了。但是,我无法提交按钮以获取 return 数据: 我有以下代码:

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= 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 post 请求并解析 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)

看来你很接近。在 Enter address here 字段中输入 locations 之前,您需要诱导 WebDriverwait,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions") 
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.mapdevelopers.com/what-county-am-i-in.php")
    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 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control#address")))
        address.clear()
        address.send_keys(query)
        driver.find_element_by_xpath("//button[@class='btn btn-default' and starts-with(@onclick, 'getUserExactLocation')]").click()
        # In the next line instead of the element with text as "County" you need to handle the "::before" / "::after" Pseudo Element
        county_result = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row county-result']")))
        listB.append(county_result.text)
    print(listB)
    
  • 控制台输出:

    ['County', 'County', 'County']