2016-01-15 56 views
0

我正在嘗試從該鏈接的職位描述和職位要求: https://www.jobsbank.gov.sg/ICMSPortal/portlets/JobBankHandler/SearchDetail.do?id=JOB-2016-0008238硒不能得到IFRAME

使用硒。這是我的代碼:

driver = webdriver.Firefox() 
url = "https://www.jobsbank.gov.sg/ICMSPortal/portlets/JobBankHandler/SearchDetail.do?id=JOB-2016-0008238" 
driver.get(url) 
iframes = driver.find_elements_by_xpath('//*[@id="divMainJobDescription"]') 
print len(iframes) 
driver.switch_to_frame(iframes) 
print driver.page_source 

我得到以下錯誤:

Traceback (most recent call last): File "D:\scraper.py", line 79, in main() File "D:\Abigail MTI Projects\Singapore Skills\JobsBankScraper\jobsbank14012016_abi.py", line 74, in main driver.switch_to_frame(iframes) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 537, in switch_to_frame self._switch_to.frame(frame_reference) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 67, in frame self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference}) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response raise exception_class(message, screen, stacktrace) NoSuchFrameException: Message: Unable to locate frame: [object Object] Stacktrace: at FirefoxDriver.prototype.switchToFrame (file:///c:/users/mti_wl~1/appdata/local/temp/tmpwelh3x/extensions/[email protected]/components/driver-component.js:10745) at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/mti_wl~1/appdata/local/temp/tmpwelh3x/extensions/[email protected]/components/command-processor.js:12551) at DelayedCommand.prototype.executeInternal_ (file:///c:/users/mti_wl~1/appdata/local/temp/tmpwelh3x/extensions/[email protected]/components/command-processor.js:12556) at DelayedCommand.prototype.execute/< (file:///c:/users/mti_wl~1/appdata/local/temp/tmpwelh3x/extensions/[email protected]/components/command-processor.js:12498)

回答

0

iframes是列表中,你需要切換到一個單一的框架。

driver.find_elements返回列表,而driver.find_element返回單個web元素(注意元素/ s中的's')。

此外,divMainJobDescriptioniframe,我相信你正在尋找frameJobDescription

iframe = driver.find_element_by_id("frameJobDescription") 
0

您的需要爲目標的iframe,切換到該幀,然後就可以開始確定其他元素,如div的,它存在於該框架內。您需要指定iframe標記本身而不是iframe中的元素。

因此,而不是像

iframes = driver.find_elements_by_xpath('//*[@id="divMainJobDescription"]') 

目標的IFRAME標籤在框架內立即靶向一個div,然後切換。 (如果你只想要一個iframe中您可以使用單數find_ 元素 _by_xpath。)

iframe = driver.find_element_by_xpath('//iframe[@id="frameJobDescription"]') 
driver.switch_to_frame(iframe) 
job_description = driver.find_element_by_xpath('//*[@id="divMainJobDescription"]') 

獎勵:您可以使用其他方式來指定的iframe,如發現由ID的元素,而不是寫的XPath ,如果你只是在看ID。

iframe = driver.find_element_by_id('frameJobDescription') 

如果您需要在腳本中獲取兩個iframe中的詳細信息,則需要輪流切換幀。