首页 > 解决方案 > 无法找到具有可见文本的元素

问题描述

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())

错误截图

点击查看页面

标签: python-3.xselenium-webdriver

解决方案


由于脚本在命令行上运行,而不是作为单个脚本运行,因此 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())

推荐阅读