简体   繁体   中英

Python Selenium - How to click element that is not a button

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: 在此处输入图片说明

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

在此处输入图片说明

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>

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

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM