【问题标题】:trying scrape this page with selenium and python尝试用 selenium 和 python 刮掉这个页面
【发布时间】:2019-11-07 01:54:00
【问题描述】:

我正在尝试使用 selenium/python 抓取此页面/iframe,但我无法在此选定表单中插入任何文本。

link

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
url = 'http://web.transparencia.pe.gov.br/despesas/despesa-geral/'
driver.get(url)
sleep(10)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
el = driver.find_element_by_xpath("//*[@id='html_selectug']")
el.click()

当我尝试获取列表框时:

el_cl = el.find_element_by_class_name('chzn-select')
el_cl.click()

引发异常

selenium.common.exceptions.ElementNotInteractableException: Message: Element <select class="chzn-select"> could not be scrolled into view

有什么建议吗?

【问题讨论】:

  • 你能告诉我为什么这么多人写“scrap”而不是“scrape”吗?抓取是将某些东西作为垃圾扔掉,抓取是从网页中提取数据。
  • 你得到什么错误?而且我没有看到任何选定的表格。
  • 当我尝试获取列表框时:'el_cl = el.find_element_by_class_name('chzn-select')' 'el_cl.click()' 引发异常 'selenium.common.exceptions.ElementNotInteractableException:消息:元素

标签: python selenium web-scraping


【解决方案1】:

类名“chzn-select”不是您尝试单击的列表的正确类(屏幕截图中指定的类)。

您可以尝试获取所有选项,然后通过如下文本单击特定选项

AllOptions = driver.find_elements_by_xpath("//*[@id='html_selectug']/select/option")
for option in AllOptions:
             if(option.get_attribute("value")=="option text you want to click")
                option.click()
                break

【讨论】:

  • 谢谢,但我不知道你有没有注意到,option.text 总是一个空字符串
  • 用 get_attribute("value") 验证它有效。如果您仍然面临问题,请告诉我。
  • 你给了我一个很好的线索。这些选项显示为非活动状态(如果要检查元素,可以看到灰色代码)。我必须使用 js 代码让它们处于活动状态。
【解决方案2】:

Amith YR 给了我一个很好的线索。这些选项是显示无效的(如果要检查元素,可以看到灰色代码)。我必须使用 js 代码让它们处于活动状态。

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
url = 'http://web.transparencia.pe.gov.br/despesas/despesa-geral/'
driver.get(url)
sleep(10)

# activate the options
js = "document.getElementById('iframe').contentWindow.document.getElementsByClassName('chzn-select')" \
     "[1].style.display = 'inline';"
driver.execute_script(js)

# now, I'm able to grab the listbox options
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
options = driver.find_elements_by_xpath("//*[@id='html_selectug']/select/option")
options[4].click()

【讨论】:

    猜你喜欢
    • 2015-04-02
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多