您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Selenium(Python) – SELECT

5b51 2022/1/14 8:23:14 python 字数 2630 阅读 577 来源 www.jb51.cc/python

现在我的脚本转到页面并从下拉列表中打开第二个对象“Vijesti”,然后才收到错误消息. 这是错误: StaleElementReferenceException: Message: Element not found in the cache – perhaps the page has changed since it was looked up 来自Selenium网站: Thrown wh

概述

这是错误

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:

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

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

总结

以上是编程之家为你收集整理的Selenium(Python) – SELECT全部内容,希望文章能够帮你解决Selenium(Python) – SELECT所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶