【问题标题】:Click on buttons with images单击带有图像的按钮
【发布时间】:2016-08-26 07:30:03
【问题描述】:

我正在尝试抓取此页面:

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;">

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

【问题讨论】:

    标签: python html selenium web-scraping web-crawler


    【解决方案1】:

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

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

    为我工作。

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

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

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 2015-02-22
      • 2012-02-14
      • 1970-01-01
      • 2012-06-24
      • 2011-08-14
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多