2013-08-25 41 views
12

Im viết tập lệnh python bằng selenium webdriver để lấy một số dữ liệu từ trang web và Tôi đang cố gắng nhấp vào nút tiếp theo trong trang web this. Trong trường hợp các nút được định nghĩa:Python Selenium với Phantomjs - Nhấp Không thành công: Tham chiếuError: Không thể tìm biến

<a id="ctl00_FullRegion_npsGridView_lnkNext" class="nextCol" href="javascript:__doPostBack('ctl00$FullRegion$npsGridView$lnkNext','')">Next</a> 

wih đoạn mã sau vào python

URL='http://www.nordpoolspot.com/Market-data1/Elspot/Area-Prices/ALL1/Hourly/' 
nextId="ctl00_FullRegion_npsGridView_lnkNext" 
browser=webdriver.PhantomJS('./phantomjs') 
browser.get(URL) 
nextBtn=browser.find_element_by_id(nextId) 
time.sleep(5) 
nextBtn.click() 

này hoạt động tốt khi sử dụng Firefox hoặc chrome Webdriver nhưng với Phantomjs tôi nhận được lỗi sau.

selenium.common.exceptions.WebDriverException: Message: u'Error Message => \'Click   
failed: ReferenceError: Can\'t find variable: __doPostBack\'\n caused by Request 

Lỗi này xuất hiện trong nhiều tìm kiếm trên google nhưng thực sự không tìm thấy cách sửa lỗi khi sử dụng ma ảo.

+0

Từ này [SO câu hỏi] (http://stackoverflow.com/questions/16863773/python-selenium-javascript-link-click-fails- để thực hiện). Hãy thử gọi phương thức 'gửi' thay thế và cho tôi biết? – Rohit

+0

đã thử điều này. cảm ơn mặc dù. gửi không đưa ra lỗi, nhưng để trống toàn bộ bảng html. –

Trả lời

32

Hãy thử gửi một tiêu đề khác nhau User-Agent:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 

user_agent = (
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " + 
    "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36" 
) 

dcap = dict(DesiredCapabilities.PHANTOMJS) 
dcap["phantomjs.page.settings.userAgent"] = user_agent 

browser = webdriver.PhantomJS(desired_capabilities=dcap) 
+0

cảm ơn! nó làm việc như một sự quyến rũ :) –

+0

Cảm ơn người đàn ông, làm việc vào những ngày này ... – Illidan

Các vấn đề liên quan