使用 Selenium Python 选择下拉列表 - 无法定位元素:{"method":"css selector","selector":"[id=
Selecting drop-down using Selenium Python - Unable to locate element: {"method":"css selector","selector":"[id=
尝试在 http://ibew.org/Tools/Construction-Jobs-Board
上自动填写搜索表单
到目前为止,我编写的代码旨在 select 'Classification' 的下拉菜单和 select 'Inside Journeyman Wireman' 值的下拉菜单。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
b = webdriver.Chrome()
b.maximize_window() #For maximizing window
b.implicitly_wait(100) #gives an implicit wait for 100 seconds
b.get("http://ibew.org/Tools/Construction-Jobs-Board")
b.find_element_by_id("TabContainerSearch_SearchTab_ddlClass").click()
selectclass = Select(b.find_element_by_id('TabContainerSearch_SearchTab_ddlClass'))
selectclass.select_by_visible_text('Inside Journeyman Wireman')
错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="TabContainerSearch_SearchTab_ddlClass"]"}
我的罪是什么? :P
我进入 Selenium 一天,进入 Python 几天。感谢您的帮助。
重点是您要识别的元素位于 iframe
标记内 - 如果您进一步向上滚动,您将在 DOM 中看到它。在使用它的任何子元素之前,您需要先将上下文切换到 iframe
。
在 b.get
行之后使用这些选项之一。
iframe=b.find_element_by_css_selector("iframe[src='http://ibew.org/jobsboard/']")
b.switch_to.frame(iframe)
selectclass = Select(b.find_element_by_id('TabContainerSearch_SearchTab_ddlClass'))
selectclass.select_by_visible_text('Inside Journeyman Wireman')
或
WebDriverWait(b, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src='http://ibew.org/jobsboard/']")))
selectclass = Select(b.find_element_by_id('TabContainerSearch_SearchTab_ddlClass'))
selectclass.select_by_visible_text('Inside Journeyman Wireman')
尽可能考虑使用 WebDriverWait
。 100 秒的隐式等待破坏了自动化的意义! WebDriverWait
需要这些导入:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
尝试在 http://ibew.org/Tools/Construction-Jobs-Board
上自动填写搜索表单到目前为止,我编写的代码旨在 select 'Classification' 的下拉菜单和 select 'Inside Journeyman Wireman' 值的下拉菜单。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
b = webdriver.Chrome()
b.maximize_window() #For maximizing window
b.implicitly_wait(100) #gives an implicit wait for 100 seconds
b.get("http://ibew.org/Tools/Construction-Jobs-Board")
b.find_element_by_id("TabContainerSearch_SearchTab_ddlClass").click()
selectclass = Select(b.find_element_by_id('TabContainerSearch_SearchTab_ddlClass'))
selectclass.select_by_visible_text('Inside Journeyman Wireman')
错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="TabContainerSearch_SearchTab_ddlClass"]"}
我的罪是什么? :P 我进入 Selenium 一天,进入 Python 几天。感谢您的帮助。
重点是您要识别的元素位于 iframe
标记内 - 如果您进一步向上滚动,您将在 DOM 中看到它。在使用它的任何子元素之前,您需要先将上下文切换到 iframe
。
在 b.get
行之后使用这些选项之一。
iframe=b.find_element_by_css_selector("iframe[src='http://ibew.org/jobsboard/']")
b.switch_to.frame(iframe)
selectclass = Select(b.find_element_by_id('TabContainerSearch_SearchTab_ddlClass'))
selectclass.select_by_visible_text('Inside Journeyman Wireman')
或
WebDriverWait(b, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src='http://ibew.org/jobsboard/']")))
selectclass = Select(b.find_element_by_id('TabContainerSearch_SearchTab_ddlClass'))
selectclass.select_by_visible_text('Inside Journeyman Wireman')
尽可能考虑使用 WebDriverWait
。 100 秒的隐式等待破坏了自动化的意义! WebDriverWait
需要这些导入:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait