点击带图片的按钮 [英] Click on buttons with images
问题描述
我试图抓取此网页:
http://www.1800contractor.com/d.HI.html
我制作了这个脚本
来自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])
这部分工作正常。
然后cl点击'开始'按钮:
button = driver.find_element_by_xpath('// * [@ id =xmdListingsSearchForm] ')
button.click()
这部分不起作用,这是我在该按钮的html处看到:
< input type =imagesrc =/ rfs / servicerequest / images /go_btn_grey_20x20.gifheight =20width =20style =position:relative; top:1px;>
所以我想问题是我没有得到实际的按钮,但是按钮图像的引用。
有一种更简单的方法来解决它 - 发送换行符在邮政编码末尾 - 它会自动提交表单:
text_box.send_keys (zip_codes [0] +\\\
)
适用于我。
如果您坚持点击按钮/按钮图像,您可以使用以下CSS选择器来匹配 src
属性值,并试图保持它的可读性:
driver.find_element_by_css_selector(input [src * = go_btn])。click ()
I'm trying to crawl this page:
http://www.1800contractor.com/d.HI.html
I made this script
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()
Basically I need to put a zip code in the search box:
zip_codes = ['30324']
text_box = driver.find_element_by_xpath('//*[@id="zip"]')
text_box.send_keys(zip_codes[0])
That part is working.
And then click in the 'Go' button:
button = driver.find_element_by_xpath('//*[@id="xmdListingsSearchForm"]')
button.click()
That part doesn't work, this is what I see at the html for that button:
<input type="image" src="/rfs/servicerequest/images/go_btn_grey_20x20.gif" height="20" width="20" style="position: relative; top: 1px;">
So I guess the problem is I'm not getting the reference to the actual button but a reference for the image of the button.
There is a simpler way to solve it - send the newline character at the end of your ZIP code - it would automatically submit the form:
text_box.send_keys(zip_codes[0] + "\n")
Works for me.
If you insist on clicking the button/"button image", you can locate it using the following CSS selector matching a part of the src
attribute value and trying to keep it readable:
driver.find_element_by_css_selector("input[src*=go_btn]").click()
这篇关于点击带图片的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!