2017-07-20 44 views
1

我有一個場景,我試圖從selenium和python的詳細頁面獲取一些數據。我是新來硒和PythonPython-Selenium使用硒在python中獲取以前列表網頁的數據

我試圖用 self.driver.execute_script(「window.history.go(-1)」)

返回到前一頁並開始獲取2,3 4記錄等,但問題是: 取1個記錄之後,發生移動到詳細信息頁面,並獲取剩餘的數據單擊事件但是,當它向後移動,從詳細信息頁面,它拋出錯誤

在cmpname =全選列表頁面.find_element_by_css_selector(「。Capsuletitle h2」)

它引發錯誤StaleElem entRefrenceException:元素沒有連接到頁面文件

基本上,我想我在清單頁面,詳細信息頁面,因爲我想從兩個頁面獲取數據的每個記錄

這裏是我的循環代碼部分

parentTab = self.driver.find_element_by_class_name("capsuleList") 
    for selectAll in parentTab.find_elements_by_class_name("bsCapsule"): 

     cmpname = selectAll.find_element_by_css_selector(".Capsuletitle h2") 
     print(cmpname.text) 

     address = selectAll.find_element_by_css_selector(".Capsuleaddress a span") 
     print(address.text) 

     telephone = selectAll.find_element_by_css_selector(".Capsuletel") 
     print(telephone.text) 

     selectAll.find_element_by_css_selector('.Capsuletitle div a').click() 
     time.sleep(20) 
     adrurl = self.driver.find_element_by_css_selector('.CapsulecallToAction a').get_attribute('href') 
     print(adrurl) 
     self.driver.execute_script("window.history.go(-1)") 
     time.sleep(20) 

的問候,而不是使用self.driver.execute_script( 「window.history.go(-1)」)

回答

0

首先,你可以使用driver.back()。至於你得到的錯誤,最常見的原因是該元素所屬的頁面已被刷新,或者用戶已經導航到另一頁面。所以我建議你在返回頁面後再次查找元素。我希望這有幫助!

相關問題