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

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

Selenium select option is unable to use list elements

问题

我正在尝试在列表中选择一个选项,但是在找到该列表的元素方面遇到了困难。

这是该网站的HTML代码:

  1. <th class="left" nowrap="">
  2. <select name="position" size="5">
  3. <option value="01">One</option>
  4. <option value="02">Two</option>
  5. <option value="03">Three</option>
  6. <option value="04" selected="">Four</option>
  7. </select>
  8. </th>

列表的默认选择是 "Four",我想选择 "One",但没有成功。

我尝试使用 By.Name:

  1. select_element = browser.find_element(By.NAME, 'position')
  2. select = Select(select_element)
  3. select.select_by_visible_text('One')

这里是错误信息:

  1. Traceback (most recent call last):
  2. File "/home/test/Documents/selection.py", line 36, in <module>
  3. select_element = browser.find_element(By.NAME, 'position')
  4. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5. File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 740, in find_element
  6. return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
  9. self.error_handler.check_response(response)
  10. File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
  11. raise exception_class(message, screen, stacktrace)
  12. 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:

  1. select_element = browser.find_element(By.CSS_SELECTOR, 'select')
  2. select = Select(select_element)
  3. select.select_by_visible_text('One')

这里是错误信息:

  1. Traceback (most recent call last):
  2. File "/home/test/Documents/selection.py", line 36, in <module>
  3. select_element = browser.find_element(By.CSS_SELECTOR, 'select')
  4. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5. File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 740, in find_element
  6. return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
  9. self.error_handler.check_response(response)
  10. File "/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
  11. raise exception_class(message, screen, stacktrace)
  12. 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:

  1. &lt;th class=&quot;left&quot; nowrap=&quot;&quot;&gt;
  2. &lt;select name=&quot;position&quot; size=&quot;5&quot;&gt;
  3. &lt;option value=&quot;01&quot;&gt;One&lt;/option&gt;
  4. &lt;option value=&quot;02&quot;&gt;Two&lt;/option&gt;
  5. &lt;option value=&quot;03&quot;&gt;Three&lt;/option&gt;
  6. &lt;option value=&quot;04&quot; selected=&quot;&quot;&gt;Four&lt;/option&gt;
  7. &lt;/select&gt;
  8. &lt;/th&gt;

the default selection on the list is "Four" and trying to select One, but no luck.

I tried using By.Name

  1. select_element = browser.find_element(By.NAME, &#39;position&#39;)
  2. select = Select(select_element)
  3. select.select_by_visible_text(&#39;One&#39;)

and here is the error:

  1. Traceback (most recent call last):
  2. File &quot;/home/test/Documents/selection.py&quot;, line 36, in &lt;module&gt;
  3. select_element = browser.find_element(By.NAME, &#39;position&#39;)
  4. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5. File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py&quot;, line 740, in find_element
  6. return self.execute(Command.FIND_ELEMENT, {&quot;using&quot;: by, &quot;value&quot;: value})[&quot;value&quot;]
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py&quot;, line 346, in execute
  9. self.error_handler.check_response(response)
  10. File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py&quot;, line 245, in check_response
  11. raise exception_class(message, screen, stacktrace)
  12. 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

  1. select_element = browser.find_element(By.CSS_SELECTOR, &#39;select&#39;)
  2. select = Select(select_element)
  3. select.select_by_visible_text(&#39;One&#39;)

here is the error:

  1. Traceback (most recent call last):
  2. File &quot;/home/test/Documents/selection.py&quot;, line 36, in &lt;module&gt;
  3. select_element = browser.find_element(By.CSS_SELECTOR, &#39;select&#39;)
  4. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5. File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py&quot;, line 740, in find_element
  6. return self.execute(Command.FIND_ELEMENT, {&quot;using&quot;: by, &quot;value&quot;: value})[&quot;value&quot;]
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py&quot;, line 346, in execute
  9. self.error_handler.check_response(response)
  10. File &quot;/home/test/.local/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py&quot;, line 245, in check_response
  11. raise exception_class(message, screen, stacktrace)
  12. 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标签下的任何选项。

以下是您可以尝试的方式:

  1. import time
  2. from selenium.webdriver import Chrome, ChromeOptions
  3. from selenium.webdriver.support.select import Select
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.wait import WebDriverWait
  7. url = '您的URL'
  8. options = ChromeOptions()
  9. options.add_argument("--start-maximized")
  10. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  11. # 创建一个Chrome浏览器实例
  12. browser = Chrome(options=options)
  13. wait = WebDriverWait(browser, 10)
  14. # 打开指定的URL
  15. browser.get(url)
  16. # 切换到带有ID“customFrame”的iframe
  17. browser.switch_to.frame(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'iframe#customFrame'))))
  18. # 查找带有名称“position”的select标签
  19. select_tag = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'select[name="position"]')))
  20. # 为select标签创建一个Select对象
  21. position = Select(select_tag)
  22. # 选择值为'01'的选项
  23. position.select_by_value('01')
  24. 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:

  1. import time
  2. from selenium.webdriver import Chrome, ChromeOptions
  3. from selenium.webdriver.support.select import Select
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.support.wait import WebDriverWait
  7. url = &#39;your URL&#39;
  8. options = ChromeOptions()
  9. options.add_argument(&quot;--start-maximized&quot;)
  10. options.add_experimental_option(&quot;excludeSwitches&quot;, [&quot;enable-automation&quot;])
  11. # Create a Chrome browser instance
  12. browser = Chrome(options=options)
  13. wait = WebDriverWait(browser, 10)
  14. # Open the specified URL
  15. browser.get(url)
  16. # switch to the iframe with the ID &quot;customFrame&quot;
  17. browser.switch_to.frame(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, &#39;iframe#customFrame&#39;))))
  18. # Find the select tag with the name &quot;position&quot;
  19. select_tag = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, &#39;select[name=&quot;position&quot;]&#39;)))
  20. # Create a Select object for the select tag
  21. position = Select(select_tag)
  22. # Select the option with the value &#39;01&#39;
  23. position.select_by_value(&#39;01&#39;)
  24. 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:

确定