javascript - 2nd 页上,javascript从下拉菜单中读取一个页面,但无法找到下拉菜单

  显示原文与译文双语对照的内容

我有一个网页。

我希望在页面顶部的每个页面上,与下拉菜单的元素相关联。

我是 selenium的新手我正在做一些初步的工作

  • 打开驱动程序
  • 将它的发送到网页
  • 选择下拉菜单
  • 只需从任意值= 2选择一个随机"姓名"
  • 到页面上,从上面获取 URL 。 打印。
  • 仅从任意值= 3错误中选择一个随机"姓名"。

我使用的代码:


from selenium import webdriver


from selenium.webdriver.common.keys import Keys


from selenium.webdriver.support.ui import Select


import time



driver = webdriver.Firefox()


driver.get("http://www.hillsproducts.com/General.aspx/en-GB/PD/a-d-canine/original/can")


select = Select(driver.find_element_by_xpath("//select[@id='productSpecifier_product']"))


value="2"


select.select_by_value(value)


print(driver.current_url)


time.sleep(10)


value="3"


select.select_by_value(value)


print(driver.current_url)



有些东西我没有得到。 我得到的错误如下:

回溯( 最近一次通话): File第 19行,select.select_by_value(value) 文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.46.1-py3.4.egg/selenium/webdriver/support/select.py", 行 76,select_by_value选项= self._el. find_elements ( 。by 。css_selector,css ) 文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.46.1-py3.4.egg/selenium/webdriver/remote/webelement.py", 行 485,在 find_elements {"using": 。 by"值":: 文件行 447,在_execute返回 self._parent.execute(command, params) 文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.46.1-py3.4.egg/selenium/webdriver/remote/webdriver.py", 行 193,执行 self.error_handler.check_response(response) 文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.46.1-py3.4.egg/selenium/webdriver/remote/errorhandler.py", 第 181行,在check_response引发 exception_class ( 消息,屏幕,stacktrace ) 消息: 元素在缓存中找不到- 或者许该页已经从查找处理器查找后发生了变化: at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9348) at Utils.getElementAt (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpr37ozu9l/extensions/fxdriver@googlecode.com/components/driver-component.js:8942) at FirefoxDriver.prototype.findElementsInternal_ (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpr37ozu9l/extensions/fxdriver@googlecode.com/components/driver-component.js:10685) at FirefoxDriver.prototype.findChildElements (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpr37ozu9l/extensions/fxdriver@googlecode.com/components/driver-component.js:10706) at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpr37ozu9l/extensions/fxdriver@googlecode.com/components/command-processor.js:12643) at DelayedCommand.prototype.executeInternal_ (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpr37ozu9l/extensions/fxdriver@googlecode.com/components/command-processor.js:12648) at DelayedCommand.prototype.execute/<(file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpr37ozu9l/extensions/fxdriver@googlecode.com/components/command-processor.js:12590)

任何想法都是值得感激的 !

alex回答后更新:

回溯( 最近一次通话): 文件"/Users/Luigi/Desktop/selenium_attempt.py", 行 18,如果索引索引> = len(select.options),则为: 文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.46.1-py3.4.egg/selenium/webdriver/support/select.py", 行 46,在选项中返回 self._el. find_elements ( 通过。tag_name'选项') 文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.46.1-py3.4.egg/selenium/webdriver/remote/webelement.py", 第 485行,在 find_elements {"using": 中 by"值":: 文件行 447,在_execute返回 self._parent.execute(command, params) 文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.46.1-py3.4.egg/selenium/webdriver/remote/webdriver.py", 行 193,执行 self.error_handler.check_response(response) 文件"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/selenium-2.46.1-py3.4.egg/selenium/webdriver/remote/errorhandler.py", 第 181行,在check_response引发 exception_class ( 消息,屏幕,stacktrace ) 消息: 元素在缓存中找不到- 或者许该页已经从查找处理器查找后发生了变化: at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9348) at Utils.getElementAt (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpzrilw39c/extensions/fxdriver@googlecode.com/components/driver-component.js:8942) at FirefoxDriver.prototype.findElementsInternal_ (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpzrilw39c/extensions/fxdriver@googlecode.com/components/driver-component.js:10685) at FirefoxDriver.prototype.findChildElements (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpzrilw39c/extensions/fxdriver@googlecode.com/components/driver-component.js:10706) at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpzrilw39c/extensions/fxdriver@googlecode.com/components/command-processor.js:12643) at DelayedCommand.prototype.executeInternal_ (file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpzrilw39c/extensions/fxdriver@googlecode.com/components/command-processor.js:12648) at DelayedCommand.prototype.execute/<(file:///var/folders/8s/hl6bx6z91yq6r81hpqg995rw0000gn/T/tmpzrilw39c/extensions/fxdriver@googlecode.com/components/command-processor.js:12590)

时间: 原作者:

每次加载新页面时,都必须使用 reinstantiate:


from selenium import webdriver


from selenium.webdriver.support.ui import Select



driver = webdriver.Firefox()


driver.get("http://www.hillsproducts.com/General.aspx/en-GB/PD/a-d-canine/original/can")



index = 0


while True:


 select = Select(driver.find_element_by_id("productSpecifier_product"))



 # exit the loop if all the options were seen


 if index> = len(select.options):


 break



 select.select_by_index(index)


 print(driver.current_url)



 index += 1



原作者:
...