python selenium中的“没有这样的元素错误”

时间:2017-04-15 15:22:02

标签: python selenium

我有一个问题,我在python上使用selenium制作测试程序 步骤

  1. 连接搜索网站。
  2. 随机点击
  3. 输入keyword1并搜索(driver.find_element_by_css_selector('#query'))
  4. 随机点击
  5. 输入keyword2并搜索< = error(driver.find_element_by_css_selector('#query'))
  6. 我不理解两个相同的元素(#query),为什么第二个只是错误。 拜托,给我。

    步骤4,6是完全相同的脚本

    # connect main(portal site)
         :           :
    
    # random click (news area)
    log('news area random click!!')
    driver.find_element_by_css_selector('div.id_news li.ut_item a').click()
    log('3sec delay')
    time.sleep(3)
    
    # move main(back)
    log('move main')
    driver.back()
    time.sleep(2)
    
    # input keyword1
    log('input keyword1 : ' + keyword1)
    element = driver.find_element_by_id('query') # is working very well
    element.send_keys(keyword1)
    element.send_keys(Keys.RETURN)
    time.sleep(3) 
    
    # random click
    log('news click !!!!!')
    try:
        driver.find_element_by_css_selector('section.sp_nnews ul.list_news li a').click()
    except NoSuchElementException:
        # if not exists news area
        log("blog click!!!")
        driver.find_element_by_css_selector('section.sp_ntotal ul.lst_total li a').click()
    
    time.sleep(2) # TODO : 17sec 
    
    # move main again
    log('move main')
    driver.back()
    time.sleep(5)
    
    # input keyword2
    log('input keyword2 : ' + keyword2)
    element = driver.find_element_by_id('query') # is not working :( 
    element.send_keys(keyword2)
    element.send_keys(Keys.RETURN)
    time.sleep(3) 
    

0 个答案:

没有答案