Search code examples
pythonselenium-webdriverwebdriverjupyter-notebookwebdriverwait

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


Here is html of the button that I wanna click on it: I think I should grab the visible one.(I guess)

 <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>

I can put the data to the search area. However, I can not submit my button to get return data: I have this code below:

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)

For submitButton, I also try:

submitButton= driver.find_element_by_css_selector('button.btnbtn-defaultxs-btn').click()

and

driver.find_element_by_xpath("//button[@class='btn btn-default xs-btn'][@type='button'][contains(., 'input-group visible-xs')]").click()

result should like this:

["Yellow Medicine County", "Yellow Medicine County", "Dodge County"]

Solution

  • Seems you were pretty close. You need to induce WebDriverwait before entering the locations within the Enter address here field and you can use the following solution:

    • Code Block:

      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)
      
    • Console Output:

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