硒(Python)-选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32382415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:28:44  来源:igfitidea点击:

Selenium (Python) - SELECT

pythonseleniumselenium-webdriver

提问by Cudoviste

Right now my script go to page and open the second object from the drop down list, "Vijesti", before I get the error message.

现在,在收到错误消息之前,我的脚本转到页面并打开下拉列表中的第二个对象“Vijesti”。

This is the error:

这是错误:

StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up

StaleElementReferenceException:消息:在缓存中未找到元素 - 可能页面自查找后已更改

From Selenium site:

从硒站点:

Thrown when a reference to an element is now “stale”. Stale means the element no longer appears on the DOM of the page. Possible causes of StaleElementReferenceException include, but not limited to:

  • You are no longer on the same page, or the page may have refreshed since the element was located.
  • The element may have been removed and re-added to the screen, since it was located. Such as an element being relocated. This can happen typically with a javascript framework when values are updated and the node is rebuilt.
  • Element may have been inside an iframe or another context which was refreshed.

当对元素的引用现在“过时”时抛出。陈旧意味着该元素不再出现在页面的 DOM 上。StaleElementReferenceException 的可能原因包括但不限于:

  • 您不再在同一页面上,或者页面可能在元素被定位后刷新。
  • 该元素可能已被删除并重新添加到屏幕上,因为它已被定位。例如一个元素被重新定位。当值更新并重建节点时,这通常会发生在 javascript 框架中。
  • 元素可能位于 iframe 或其他刷新的上下文中。

What I want to select each object, and open it.

我想选择每个对象,然后打开它。

This is the SELECT part from the url:

这是来自 url 的 SELECT 部分:

<select id="kategorija" name="kategorija">
<option value="0">Kategorija</option>
<option value="12">Vijesti</option>
<option value="8">Biznis</option>
<option value="5">Sport</option>
<option value="2">Magazin</option>
<option value="7">Lifestyle</option>
<option value="3">Scitech</option>
<option value="6">Auto</option> 
</select>

Code:

代码:

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.klix.ba/") 

assert "Klix" in driver.title 

elem = driver.find_element_by_name("q") 

elem.send_keys("test") 

elem.send_keys(Keys.RETURN)


select = Select(driver.find_element_by_name('kategorija'))

all_options = [o.get_attribute('value') for o in select.options]

for x in all_options:
    select.select_by_value(x)
    time.sleep(3)

This is the urlwhere I do my testings.

这是我进行测试的网址

采纳答案by alecxe

The page refreshes itselfwhen an item is chosen from the dropdown.

当从下拉列表中选择一个项目时,页面会自行刷新

You need to "refind" the selectelement on each option select:

您需要select在每个选项选择上“重新查找”元素:

select = Select(driver.find_element_by_name('kategorija'))

for index in range(len(select.options)):
    select = Select(driver.find_element_by_name('kategorija'))
    select.select_by_index(index)

    # grab the results