我正在尝试抓取此页面:
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/