简体   繁体   English

Python Selenium - 如何单击不是按钮的元素

[英]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.我在 python 中使用 selenium 并尝试单击一个不是按钮类的元素。 I'm using Google Chrome as my browser/web driver我使用 Google Chrome 作为我的浏览器/网络驱动程序

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.我可以解析结果 HTML(例如,Beautiful Soup),但我想点击它们。 If I inspect the first row of elements, I see this is kept in a div element, with a style of "margin-left:3px;".如果我检查第一行元素,我会看到它保存在一个 div 元素中,样式为“margin-left:3px;”。

But this is not a button element, so the normal click() function does not work.但这不是按钮元素,因此普通的 click() 函数不起作用。 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.编辑:更准确地说,很可能你试图点击<tr>标签内的 div 元素。 while the <tr> tag contains javascript:selectSearchRow('../Datalets/Datalet.aspx?sIndex=1&idx=1') so your script should click this tag not <div><tr>标签包含javascript:selectSearchRow('../Datalets/Datalet.aspx?sIndex=1&idx=1')所以你的脚本应该点击这个标签而不是<div>

Clicking the first row with xpath - see below.使用 xpath 单击第一行 - 见下文。 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.诱导WebDriverWait和以下css选择器单击表项。

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:浏览器快照:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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