0

I am trying to copy data from the following website: https://imljail.shelbycountytn.gov/IML.

The site has two buttons, both labelled "Search." The button I want to press has the following code:

<table width="300" border="0" align="right" cellpadding="0" cellspacing="0">
<tr align="right" style="color: #000000;"> 
<td><a href = "javascript:setNameSearch()" onkeydown="keyDownNameSearch()"><img src = "../images/button_up_search.gif" alt = "Search Button" /></a> <a href = "javascript:resetNameSearch()" onkeydown="keyDownNameReset()"><img src = "../images/button_up_reset.gif" alt = "Reset Button" /></a></td></tr></table> 

I don't need to fill in any of the fields - all the data I want appears when you leave the name fields blank and just click the button.

I've been using Selenium and have been able to locate/load the page, but can't get it to find the right element and click it. Any ideas of how to specify this button? Thanks!

import pandas as pd
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('URL...)
driver_find_element(By. [tried ID, LINK_TEXT, PARTIAL_LINK_TEXT, etc.]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

Given the HTML:

<table width="300" border="0" align="right" cellpadding="0" cellspacing="0">
    <tr align="right" style="color: #000000;"> 
        <td>
            <a href = "javascript:setNameSearch()" onkeydown="keyDownNameSearch()">
                <img src = "../images/button_up_search.gif" alt = "Search Button" />
            </a> 
            <a href = "javascript:resetNameSearch()" onkeydown="keyDownNameReset()">
                <img src = "../images/button_up_reset.gif" alt = "Reset Button" />
            </a>
        </td>
    </tr>
</table>

To click on the <a> element with attribute alt = "Search Button" you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='setNameSearch'] > img[alt='Search Button']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'setNameSearch')]/img[@alt='Search Button']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352