英文:
Selenium select option is unable to use list elements
问题
我正在尝试在列表中选择一个选项,但是在找到该列表的元素方面遇到了困难。
这是该网站的HTML代码:
<th class="left" nowrap="">
<select name="position" size="5">
<option value="01">One</option>
<option value="02">Two</option>
<option value="03">Three</option>
<option value="04" selected="">Four</option>
</select>
</th>
列表的默认选择是 "Four",我想选择 "One",但没有成功。
我尝试使用 By.Name:
select_element = browser.find_element(By.NAME, 'position')
select = Select(select_element)
select.select_by_visible_text('One')
这里是错误信息:
Traceback (most recent call last):
File "/home/test/Documents/selection.py", line 36, in <module>
select_element = browser.find_element(By.NAME, 'position')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 740, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="position"]; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
我还尝试使用 By.CSS_SELECTOR:
select_element = browser.find_element(By.CSS_SELECTOR, 'select')
select = Select(select_element)
select.select_by_visible_text('One')
这里是错误信息:
Traceback (most recent call last):
File "/home/test/Documents/selection.py", line 36, in <module>
select_element = browser.find_element(By.CSS_SELECTOR, 'select')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 740, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: select; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
英文:
I am trying to select one on a bunch of options on the list, however I am stuck on finding the element for that list.
here is the html code for that website:
<th class="left" nowrap="">
<select name="position" size="5">
<option value="01">One</option>
<option value="02">Two</option>
<option value="03">Three</option>
<option value="04" selected="">Four</option>
</select>
</th>
the default selection on the list is "Four" and trying to select One, but no luck.
I tried using By.Name
select_element = browser.find_element(By.NAME, 'position')
select = Select(select_element)
select.select_by_visible_text('One')
and here is the error:
Traceback (most recent call last):
File "/home/test/Documents/selection.py", line 36, in <module>
select_element = browser.find_element(By.NAME, 'position')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 740, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="position"]; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
I also tried using By.CSS_SELECTOR
select_element = browser.find_element(By.CSS_SELECTOR, 'select')
select = Select(select_element)
select.select_by_visible_text('One')
here is the error:
Traceback (most recent call last):
File "/home/test/Documents/selection.py", line 36, in <module>
select_element = browser.find_element(By.CSS_SELECTOR, 'select')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 740, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
self.error_handler.check_response(response)
File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: select; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
答案1
得分: 0
基于您提供的HTML,很明显选项位于一个select
标签下。我们可以使用Selenium的Select
对象轻松选择select
标签下的任何选项。
以下是您可以尝试的方式:
import time
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
url = '您的URL'
options = ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
# 创建一个Chrome浏览器实例
browser = Chrome(options=options)
wait = WebDriverWait(browser, 10)
# 打开指定的URL
browser.get(url)
# 切换到带有ID“customFrame”的iframe
browser.switch_to.frame(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe#customFrame'))))
# 查找带有名称“position”的select标签
select_tag = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'select[name="position"]')))
# 为select标签创建一个Select对象
position = Select(select_tag)
# 选择值为'01'的选项
position.select_by_value('01')
time.sleep(5)
希望这可以帮助您使用Selenium选择select
标签中的选项。
英文:
Based on your given HTML, It's very clear that the options are under a select
tag. And we can easily choose any options under a select tag using Selenium's Select
object.
Here's how you may try:
import time
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
url = 'your URL'
options = ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
# Create a Chrome browser instance
browser = Chrome(options=options)
wait = WebDriverWait(browser, 10)
# Open the specified URL
browser.get(url)
# switch to the iframe with the ID "customFrame"
browser.switch_to.frame(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe#customFrame'))))
# Find the select tag with the name "position"
select_tag = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'select[name="position"]')))
# Create a Select object for the select tag
position = Select(select_tag)
# Select the option with the value '01'
position.select_by_value('01')
time.sleep(5)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论