2017-05-09 69 views
0
# -*- coding: utf-8 -* 
    import scrapy 
    import os 
    from selenium import webdriver 
    import time 


    def get_path(): 
     path = os.path.abspath(
      os.path.join(
       os.path.dirname(
        os.path.abspath(__file__)), 
       os.path.pardir)) 
     return os.path.join(
      path, 'tool/phantomjs-2.1.1-linux-x86_64/bin/phantomjs') 

    browser = webdriver.PhantomJS(get_path()) 
    browser.get('http://www.chinaccm.com/37/372002list.shtml') 
    elem = browser.find_element_by_id('keyword') 
    elem.clear() 
    time.sleep(1) 
    elem.send_keys('針葉漿'.decode('utf-8')) 
    browser.find_element_by_xpath(
     '//div[@class="search0013"]').click() 
    time.sleep(1) 
    print('----------------------{}'.format(browser.current_url)) 

輸出---------------------- http://www.chinaccm.com/37/372002list.shtmlPhantomJS沒有跳到下一個頁面在Python硒

點擊似乎沒有工作 但是當我使用的chromedriver,它的工作原理

# -*- coding: utf-8 -* 
    import scrapy 
    import os 
    from selenium import webdriver 
    import time 


    def get_path(): 
     path = os.path.abspath(
      os.path.join(
       os.path.dirname(
        os.path.abspath(__file__)), 
       os.path.pardir)) 
     return os.path.join(
      path, 'tool/chromedriver') 

    browser = webdriver.Chrome(get_path()) 
    browser.get('http://www.chinaccm.com/37/372002list.shtml') 
    elem = browser.find_element_by_id('keyword') 
    elem.clear() 
    time.sleep(1) 
    elem.send_keys('針葉漿'.decode('utf-8')) 
    browser.find_element_by_xpath(
     '//div[@class="search0013"]').click() 
    time.sleep(1) 
    print('----------------------{}'.format(browser.current_url)) 

輸出是 ---------------------- http://www.chinaccm.com/WebInfoList.aspx?start_date=2016-5-9&end_date=2017-5-9&keyword=%E9%92%88%E5%8F%B6%E6%B5%86&Column=372002

+0

嘗試在點擊'driver.save_screenshot('screen_hires.png')'之前保存phantomjs的屏幕截圖,當解析度不同導致某些元素隱藏在某個下拉列表中時,我曾經遇到過它(因爲phantomjs默認爲移動視圖)。如果是這樣,你可以使用'driver.set_window_size(1400,1000)' –

回答

0

將您的xpath更改爲'//div[@class="search0013"]/input'

Chrome和PhantomJS並不總是以相同的方式處理事情。使用'//div[@class="search0013"]您正在選擇包含與Chrome配合使用的按鈕的div元素,因爲點擊發送至按鈕所在的位置。 PhantomJS可能會在div內的其他地方發送點擊並錯過按鈕。

+0

它的工作原理!十分感謝 – TtC