Selenium选择选项无法使用列表元素。

huangapple go评论70阅读模式
英文:

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:

&lt;th class=&quot;left&quot; nowrap=&quot;&quot;&gt;
            &lt;select name=&quot;position&quot; size=&quot;5&quot;&gt;  
            	&lt;option value=&quot;01&quot;&gt;One&lt;/option&gt;
            	&lt;option value=&quot;02&quot;&gt;Two&lt;/option&gt;
            	&lt;option value=&quot;03&quot;&gt;Three&lt;/option&gt;
            	&lt;option value=&quot;04&quot; selected=&quot;&quot;&gt;Four&lt;/option&gt;
            &lt;/select&gt;
&lt;/th&gt;

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, &#39;position&#39;)
select = Select(select_element)
select.select_by_visible_text(&#39;One&#39;)

and here is the error:

Traceback (most recent call last):
  File &quot;/home/test/Documents/selection.py&quot;, line 36, in &lt;module&gt;
    select_element = browser.find_element(By.NAME, &#39;position&#39;)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py&quot;, line 740, in find_element
    return self.execute(Command.FIND_ELEMENT, {&quot;using&quot;: by, &quot;value&quot;: value})[&quot;value&quot;]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py&quot;, line 346, in execute
    self.error_handler.check_response(response)
  File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py&quot;, line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name=&quot;position&quot;]; 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, &#39;select&#39;)
select = Select(select_element)
select.select_by_visible_text(&#39;One&#39;)

here is the error:

Traceback (most recent call last):
  File &quot;/home/test/Documents/selection.py&quot;, line 36, in &lt;module&gt;
    select_element = browser.find_element(By.CSS_SELECTOR, &#39;select&#39;)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py&quot;, line 740, in find_element
    return self.execute(Command.FIND_ELEMENT, {&quot;using&quot;: by, &quot;value&quot;: value})[&quot;value&quot;]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py&quot;, line 346, in execute
    self.error_handler.check_response(response)
  File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py&quot;, 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 = &#39;your URL&#39;

options = ChromeOptions()
options.add_argument(&quot;--start-maximized&quot;)
options.add_experimental_option(&quot;excludeSwitches&quot;, [&quot;enable-automation&quot;])

# 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 &quot;customFrame&quot;
browser.switch_to.frame(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, &#39;iframe#customFrame&#39;))))

# Find the select tag with the name &quot;position&quot;
select_tag = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, &#39;select[name=&quot;position&quot;]&#39;)))

# Create a Select object for the select tag
position = Select(select_tag)

# Select the option with the value &#39;01&#39;
position.select_by_value(&#39;01&#39;)

time.sleep(5)

huangapple
  • 本文由 发表于 2023年7月14日 08:34:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76684032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定