Python selenium不会切换帧

时间:2015-09-28 18:57:37

标签: python selenium selenium-webdriver

当我跑步时

from selenium import webdriver

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

driver.switch_to.frame(0)

elem = driver.find_element_by_id("selectfilebox")
elem.click()

from selenium import webdriver

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

frame_name = driver.find_elements_by_xpath("/html/frameset/frame").get_attribute("name")

driver.switch_to.frame(frame_name)

elem = driver.find_element_by_id("selectfilebox")
elem.click()

它们都继续运行而没有回溯,但鼠标停留在地址栏中。

当我将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/fxdriver@googlecode.com/components/driver-component.js:10717)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpyickpa63/extensions/fxdriver@googlecode.com/components/command-processor.js:12617)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpyickpa63/extensions/fxdriver@googlecode.com/components/command-processor.js:12622)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpyickpa63/extensions/fxdriver@googlecode.com/components/command-processor.js:12564)

1 个答案:

答案 0 :(得分:4)

首先,您正确切换到相应的val selects = cols.map(c => s"(select $c as value,sum(1) as count from sometable group by $c)") val transformedString = selects.head + selects.tail.mkString(" a union "," union ","")

您的实际问题是您不必单击上传框 - 它会触发您无法控制的文件上传弹出窗口

如果要上传文件,则需要直接“发送密钥”文件输入:

iframe

完整的工作代码:

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