【问题标题】:Webscraping from pop up using python selenium使用 python selenium 从弹出窗口中抓取网页
【发布时间】: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


    【解决方案1】:

    这些元素被包裹在一个框架中,所以你应该先切换到那个框架。 这段代码应该可以工作:

    #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")
    time.sleep(5)
    driver.switch_to.frame(driver.find_element_by_xpath("//*[@id='CourseFrame']"))
    #print(driver.page_source)
    time.sleep(5)
    try:            
        pop_up=WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//*[@id='RepeaterMain']/tbody/tr[1]/td/h3")))
        print(pop_up.text)
    except NoSuchElementException:
        pass
    

    输出:

    AAAS 390  Special Topics in Black/Africana Studies
    

    【讨论】:

    • 感谢 Huynh,这行得通!关于提取数据后如何关闭弹出窗口的任何想法?我尝试找到关闭按钮的 xpath 并将其包含在 print(pop_up.text) 语句之后,但它抛出 NoSuchElementException(我们在这里传递异常)
    • 您应该在点击关闭按钮之前切换出框架。像这样:driver.switch_to.default_content()
    • 您是如何将 xpath 设为 "//*[@id='CourseFrame']" 的?
    【解决方案2】:

    弹出模式中有一个框架。您必须先切换到该框架,然后才能访问框架内的元素。

    试试这个:

    #Trying to Scrape from the popup
    driver.switch_to.frame(driver.find_element_by_id("CourseFrame"))
    pop_up=driver.find_element_by_xpath("//*[@id='RepeaterMain']")
    print(pop_up.text)
    

    【讨论】:

    • 感谢 Ahmed,但它会抛出 NoSuchFrameException,但我明白了切换帧的意义。
    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 2022-01-14
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    相关资源
    最近更新 更多