2015-09-28 64 views
1

Python的硒不會切換幀

當我將fram索引更改爲1時,我得到以下內容。

Traceback (most recent call last): 
    File "/home/ro/selem.py", line 6, in <module> 
    driver.switch_to.frame(1) 
    File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/switch_to.py", line 67, in frame 
    self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference}) 
    File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute 
    self.error_handler.check_response(response) 
    File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.NoSuchFrameException: Message: Unable to locate frame: 1 
Stacktrace: 
    at FirefoxDriver.prototype.switchToFrame (file:///tmp/tmpyickpa63/extensions/[email protected]/components/driver-component.js:10717) 
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpyickpa63/extensions/[email protected]/components/command-processor.js:12617) 
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpyickpa63/extensions/[email protected]/components/command-processor.js:12622) 
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpyickpa63/extensions/[email protected]/components/command-processor.js:12564) 

回答

4

首先,您正確地切換到適當的iframe

你的實際問題是,你不必點擊上傳框 - 它會觸發一個文件上傳彈出窗口,你根本無法控制

如果你想上傳一個文件,你需要直接「發送鍵」輸入的文件:

upload_input = driver.find_element_by_css_selector("#selectfilebox input[type=file]") 
upload_input.send_keys("/absolute/path/to/the/file/to/upload") 

完整的工作代碼:

from selenium import webdriver 

driver = webdriver.Firefox() 
driver.get("http://www.speedyshare.com/") 

driver.switch_to.frame(0) 

upload_input = driver.find_element_by_css_selector("#selectfilebox input[type=file]") 
upload_input.send_keys("/Users/user/Downloads/dr-evil-and-minion-laughing.png") 

upload_button = driver.find_element_by_link_text("Upload") 
upload_button.click() 
+0

感謝的人,這個工作。 – booberz