繁体   English   中英

使用 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 的“分类”下拉菜单和 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.

相关问题 收到消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"[id="None"]"} Python 使用 selenium 时 无法找到硒中的元素:{“方法”:“ css选择器”,“选择器”:“ [id =” identifierId”]”} 无法使用python在硒中使用CSS_SELECTOR定位元素 Python: Selenium 没有这样的元素: 无法定位元素: {“method”:“xpath”,“selector”:“//button[@data-id=1]”} selenium 无法使用方法定位元素:无法通过 id、css_selector、xpath、链接文本找到元素 selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“.ui流感~”} 消息:没有这样的元素:无法找到元素:{“method”:“css selector”,“selector”:“[id=”loginUsername“]”} 没有这样的元素:无法定位元素:{“method”:“css selector”,“selector”:“[id=”guided-tour-tag-credentials-new“]”} NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":"[id="events_table"]"} Selenium NoSuchElementException-无法找到元素:{“方法”:“ css选择器”,“选择器”:“ [[名称=”电子邮件地址”]”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM