python - 单击带有图像的按钮

标签 python html selenium web-scraping web-crawler

我正在尝试抓取此页面:

http://www.1800contractor.com/d.HI.html

我做了这个脚本

from selenium import webdriver


URL = "http://www.1800contractor.com/d.GA.html"
zip_codes = ['30324']

driver = webdriver.Firefox()
driver.get(URL)

zip_codes = ['30324']
text_box = driver.find_element_by_xpath('//*[@id="zip"]')
text_box.send_keys(zip_codes[0])        
button = driver.find_element_by_xpath('//*[@id="xmdListingsSearchForm"]')
button.click()

基本上我需要在搜索框中输入邮政编码:

zip_codes = ['30324']
text_box = driver.find_element_by_xpath('//*[@id="zip"]')
text_box.send_keys(zip_codes[0])

那部分工作正常。

然后单击“开始”按钮:

button = driver.find_element_by_xpath('//*[@id="xmdListingsSearchForm"]')
button.click()

那部分不起作用,这是我在该按钮的 html 中看到的:

<input type="image" src="/rfs/servicerequest/images/go_btn_grey_20x20.gif" height="20" width="20" style="position: relative; top: 1px;">

所以我想问题是我没有得到对实际按钮的引用,而是对按钮图像的引用。

最佳答案

有一个更简单的方法来解决它 - 在您的邮政编码末尾发送 换行符 - 它会自动提交表单:

text_box.send_keys(zip_codes[0] + "\n")

对我有用。

如果你坚持点击按钮/“按钮图像”,你可以使用下面的 CSS 选择器匹配 src 属性值的一部分来定位它,并尽量保持它的可读性:

driver.find_element_by_css_selector("input[src*=go_btn]").click()

关于python - 单击带有图像的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36967097/

相关文章:

python - as_strided : Linking stepsize (strides of conv2d) with as_strided strides parameter

html - 使用 typeScript 滚动到我的 webView 上的 x,y 坐标

html - 固定 3 列站点

html - CSS 在桌面和 Android 上的 chrome 上工作正常,但在 safari 上不起作用

python - Selenium 使用 .find 方法返回空列表,即使有项目

python - 在 BioPython 中使用 Entrez 从 GenBank 检索和解析蛋白质序列

python - 将流水线与 sklearn 一起使用

python - 如何点击带有图像的onclick链接? Python Selenium

python - 修复 Excel 文件

java - 如何使用仅带有 SRC 的 Selenium 单击图像