单击带有图像的按钮
Click on buttons with images
我正在尝试抓取此页面:
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])
那部分工作正常。
然后单击 'Go' 按钮:
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")
适合我。
如果您坚持点击按钮/"button image",您可以使用以下 CSS 选择器匹配 src
属性值的一部分并尝试保持其可读性来定位它:
driver.find_element_by_css_selector("input[src*=go_btn]").click()
我正在尝试抓取此页面:
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])
那部分工作正常。
然后单击 'Go' 按钮:
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")
适合我。
如果您坚持点击按钮/"button image",您可以使用以下 CSS 选择器匹配 src
属性值的一部分并尝试保持其可读性来定位它:
driver.find_element_by_css_selector("input[src*=go_btn]").click()