简体   繁体   English

如何使用 selenium WebDriver python 遍历每个下拉列表

[英]How to traverse each dropdown list using selenium WebDriver python

I have the web result with the html tags as below, I need to loop each value and get the results from the table我的网页结果带有如下 html 标签,我需要循环每个值并从表中获取结果

 <div class = "divList"> <select class="form selectList"> <option value="3710">Thu, 04 Nov 2021</option> <option value="3709">Mon, 01 Nov 2021</option> <option value="3708">Mon, 01 Nov 2021</option> .... </select> </div>

dropdownlist = driver.find_element_by_class_name('divList')
valueslist = (dropdownlist.text).splitlines()
print(valueslist)
sel = Select(driver.find_element_by_xpath("//select[@class='form selectList']"))
for value in valueslist:
   print(value)
   sel.select_by_visible_text(value)
   print('Processing - ', value)
   time.sleep(3)

Getting the below error while changing the value after 2 iteration在 2 次迭代后更改值时出现以下错误

Thu, 04 Nov 2021
Processing -  Thu, 04 Nov 2021
Mon, 01 Nov 2021
Processing -  Mon, 01 Nov 2021
Thu, 28 Oct 2021

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document selenium.common.exceptions.StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档

try this:尝试这个:

dropdownlist = driver.find_element_by_class_name('divList')
valueslist = (dropdownlist.text).splitlines()
print(valueslist)
sel = Select(driver.find_element_by_xpath("//select[@class='form selectList']"))

for value in valueslist:
    sel = Select(driver.find_element_by_xpath("//select[@class='form selectList']"))
    sel.select_by_visible_text(value)
    print('Processing - ', value)
    time.sleep(3)

you could simplify if you want by:如果您愿意,您可以通过以下方式简化:

sel = Select(driver.find_element_by_xpath("//select[@class='form selectList']"))
nbritems = len(sel.options)
for i in range(0, nbritems):
    sel = Select(driver.find_element_by_xpath("//select[@class='form selectList']"))
    txt = sel.options[i].text
    sel.select_by_visible_text(txt)
    print('Processing - ', txt)
    time.sleep(3)

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document selenium.common.exceptions.StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档

The literal meaning is about , The referenced element is out of date , No longer attached to the current page .字面意思是about,引用的元素已经过期,不再附加到当前页面。 Usually , This is because the page has been refreshed or skipped , The solution is , Reuse findElement or findElements Method to locate the element .通常,这是因为页面被刷新或跳过了,解决方法是,重用 findElement 或 findElements 方法来定位元素

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有Webdriver的Selenium Python打印下拉列表中存在的月份列表 - How to print list of months present in dropdown list using Selenium Python with Webdriver 如何使用Python / Selenium webdriver处理Angularjs / Javascript下拉列表? - How to handle Angularjs / Javascript dropdown using Python / Selenium webdriver? Selenium Webdriver - Python - leboncoin (classified) - pb 选择下拉列表和框 - Selenium Webdriver - Python - leboncoin (classified) - pb to select a dropdown list & box 如何使用 Python 的 Selenium Webdriver 扩展列表 - How to Expand a List with Selenium Webdriver for Python 无法使用 webdriver python selenium 为组合框设置下拉值 - Unable to set dropdown values for combobox using webdriver python selenium 使用Python从Selenium WebDriver的下拉列表中选择一个元素 - Selecting an element from the dropdown on selenium webdriver using Python 如何使用Selenium WebDriver单击或选择Python中输入下拉框的第一个元素(值) - how to click or select on very first element(value) of input dropdown box in python using selenium webdriver 如何在 Python 中使用 Selenium 单击下拉列表的 li 元素^ - How to click on li element of a dropdown list using Selenium in Python^ 如何在python3中使用selenium在javascrip站点中Select下拉列表? - How to Select a dropdown list in a javascrip site using selenium in python3? Python Selenium WebDriver下拉菜单如何选择项目 - Python selenium webdriver dropdown menu how to select items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM