![](/img/trans.png)
[英]Getting Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="None"]"} Python when using selenium
[英]Selecting drop-down using Selenium Python - Unable to locate element: {“method”:“css selector”,“selector”:"[id=
尝试自动填写http://ibew.org/Tools/Construction-Jobs-Board上的搜索表单
到目前为止,我编写的代码旨在 select 的“分类”下拉菜单和 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"]"}
我的罪是什么? : PI 是一天到 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.