1

I'm using selenium in python and trying to click an element that is not a button class. I'm using Google Chrome as my browser/web driver

Here is my code:

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Chrome(executable_path="/Users/ep9k/Desktop/SeleniumTest/drivers/chromedriver")

driver.get('http://tax.watgov.org/WataugaNC/search/commonsearch.aspx?mode=address')
driver.find_element_by_name('btAgree').click()             #clicks 'Agree' button to agree to site's terms                                            

driver.find_element_by_name('inpNumber').send_keys('190')
driver.find_element_by_name('inpStreet').send_keys('ELI HARTLEY')
driver.find_element_by_name('btSearch').click()

This takes me to this page: enter image description here

I can parse the results HTML (with Beautiful Soup for example), but I want to Click on them. If I inspect the first row of elements, I see this is kept in a div element, with a style of "margin-left:3px;".

But this is not a button element, so the normal click() function does not work. Is there a way to click on this? For example, If I click on the first row of results, I am taken to this page with more information (which is what I really want):

enter image description here

3 Answers 3

4

The element doesn't need to be a button to be clickable.

after I ran your code, I've added:

results = driver.find_elements_by_class_name('SearchResults')
first_result = results[0]
first_result.click()

And it worked perfectly fine for me.

Most probably you tried to click on some different element and that's why it didn't work

EDIT: Just to be more precise, most probably you tried to click on a div element inside <tr> tag. while the <tr> tag contains javascript:selectSearchRow('../Datalets/Datalet.aspx?sIndex=1&idx=1') so your script should click this tag not <div>

Sign up to request clarification or add additional context in comments.

2 Comments

This works, thanks! One more question. How did you know to use the class name 'SearchResults'? When I look at the page source it is not clear to me how you chose this instead of the individual <div> tags for each result
@ErichPurpur I just started inspecting from the whole table and going to table children tags then I noticed that <tr> tag contains onclick attribute so I knew that this element should be clickable, then I noticed that rows with results contain that class.
1

induce WebDriverWait and following css selector to click table item.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
driver = webdriver.Chrome(executable_path="/Users/ep9k/Desktop/SeleniumTest/drivers/chromedriver")
driver.get('http://tax.watgov.org/WataugaNC/search/commonsearch.aspx?mode=address')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"btAgree"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"inpNumber"))).send_keys('190')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"inpStreet"))).send_keys('ELI HARTLEY')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"btSearch"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"tr.SearchResults"))).click()

Browser snapshot:

enter image description here

Comments

1

Clicking the first row with xpath - see below. Assuming you want to parse each of the results(parcels) after that, make use of the navigation buttons; this is a structure you could use:

table = driver.find_elements_by_xpath("//table[@id='searchResults']")
table[0].click()

#  Extract the total number of parcels from string e.g. "1 of 24"
string=driver.find_element_by_xpath("//input[@name='DTLNavigator$txtFromTo']").get_attribute('value')
#  split string in separate words; last word i.e. [-1] is the total number of parcels e.g. "24"
total_parcels=string.split(' ')[-1]

for record in range(int(total_parcels)):
    # >>> parse record here <<<
    driver.find_element_by_xpath("//input[@name='DTLNavigator$imageNext']").click()
    time.sleep(0.5) # be considerate to your source and don't load their server with numerous quick requests

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.