gpt4 book ai didi

Python selenium 不会切换帧

转载 作者:太空宇宙 更新时间:2023-11-04 10:19:59 25 4
gpt4 key购买 nike

当我运行时

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()

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

当我将帧索引更改为 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)

最佳答案

首先,您正在正确切换到适当的 iframe

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

如果要上传文件,需要直接将文件输入“发送 key ”:

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()

关于Python selenium 不会切换帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com