加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Selenium(Python) – SELECT

发布时间:2020-12-16 23:35:50 所属栏目:Python 来源: Thrown wh" />
现在我的脚本转到页面并从下拉列表中打开第二个对象“Vijesti”,然后才收到错误消息.

这是错误:

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

来自Selenium网站:

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.

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

这是来自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>

码:

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)

这是我做测试的url.

解决方法

从下拉列表中选择项目时,页面会自动刷新.

您需要“重新”选择每个选项上的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
    站长推荐
    热点阅读