【发布时间】:2019-06-30 05:31:56
【问题描述】:
我正在尝试从网站上抓取课程数据:'https://schedule.msu.edu/'。选择学期、科目并单击“查找课程”按钮后,将显示课程列表并单击每门课程,例如:(学期中的课程 AAAS 100:2019 年秋季,主题:非裔美国人和非洲研究),出现一个弹出窗口,我尝试使用 selenium 从弹出窗口中获取数据,它抛出一个异常说“NoSuchElementException:消息:没有这样的元素:无法找到元素:”。弹出窗口打开后,它是一个不同的 URL,但我无法弄清楚如何从弹出窗口中获取数据。对于此事,我将不胜感激。
这是一个使用 selenium 的示例代码:
driver = webdriver.Chrome()
driver.get("https://schedule.msu.edu/")
check_box=driver.find_element_by_xpath("//*[@id='MainContent_chkAllonePg']").click()
#clicking on the "terms"
term=driver.find_element_by_xpath("//*[@id='MainContent_ddlTerm']")
term.click()
#selecting a term
term_op=driver.find_element_by_xpath("//*[@id='MainContent_ddlTerm']/option[3]")
term_op.click()
#selecting the subject
elem_sub=driver.find_element_by_xpath("//*[@id='MainContent_ddlSubject']").click()
subject=driver.find_element_by_xpath("//*[@id='MainContent_ddlSubject']/option[1]")
subject.click()
#Clicking on 'Find Courses' button
elem_search=driver.find_element_by_xpath("//*[@id='MainContent_btnSubmit']")
elem_search.click()
#Clicking on a course to get the popup
course=driver.find_element_by_xpath("//*[@id='MainContent_divHeader1_va']/h3[1]/a").click()
#Trying to Scrape from the popup
pop_up=driver.find_element_by_xpath("//*[@id='RepeaterMain']/tbody/tr[1]/td/h3")
pop_up.click()
【问题讨论】:
标签: python-3.x selenium web-scraping