python-3.x - 无法找到具有可见文本的元素
问题描述
View source When city name selected then it has to match input campus name from the list and to be selected matched one.Below is the code and screenshot.
tkt_campus = browser.find_element_by_css_selector('#cityTicketingId')
city = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.ID, 'cityTicketingId')))
city.click()
t_city = Select(city)
t_city.select_by_visible_text(tkt_city_name.strip())
WebDriverWait(browser, 1)
tkt_campus = city = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'campusTicketingId')))
t_campus = Select(tkt_campus)
t_campus.select_by_visible_text(campus_name.strip())
解决方案
由于脚本在命令行上运行,而不是作为单个脚本运行,因此 webdriver 很可能需要等待下拉菜单呈现。当您通过命令行逐行插入时,会有一个自然的停顿,但是当作为脚本运行时,代码行会立即执行并在文本呈现为 HTML 之前检查文本。
一切都很好,除了倒数第二行。看起来您已经使用过一次 WebDriverWait 函数:
tkt_campus = browser.find_element_by_css_selector('#cityTicketingId')
city = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.ID, 'cityTicketingId')))
city.click()
t_city = Select(city)
t_city.select_by_visible_text(tkt_city_name.strip())
WebDriverWait(browser, 1)
tkt_campus = city = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'campusTicketingId')))
t_campus = Select(tkt_campus)
t_campus = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.LINK_TEXT('ELURU'))
t_campus.select_by_visible_text(campus_name.strip())
推荐阅读
- python - Python - 使用分隔符从文本文件中拆分数据
- ruby-on-rails - 在 Rails ActiveAdmin 中预先检查多个复选框
- java - ContextCompat.getDrawable() 的上下文参数应该是什么?
- java - RAM 过度支出 - JavaFX
- python - 使用 n_jobs > 1 时关闭 scikit-learn 的警告
- .net-core - '找不到请求的对象' 将 * 私钥 * 加载到 .Net Core 中的 X509Certificate2 时出错
- mysql - Mysql:对大字符串的“唯一”约束
- ag-grid - ag-grid 列标题不会通过拖动来调整大小
- typo3 - Typo3 用户权限继承不正确
- python-3.x - 如何使用 python 脚本在 SQL Server 数据库中动态插入变量值