找不到具有可见文本的元素

时间:2018-07-06 09:50:23

标签: python-3.x selenium-webdriver

View source选择城市名称时,它必须与列表中输入的校园名称匹配,并且要与之匹配。下面是代码和屏幕截图。

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

Error screenshot

Click to view page

1 个答案:

答案 0 :(得分:0)

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