如何使用python从selenium中的Auto Suggestion列表中选择一个值

时间:2017-09-06 19:21:48

标签: python selenium

我想在填写数据后选择第一个自动建议值,但它对我不起作用 WebDriverWait(driver, 10).until(driver.find_element_by_xpath("//*[@id='txtFrom']").send_keys("Delhi, India",Keys.DOWN))

我尝试使用Keys.TABKeys.DOWN两种方式,但当我使用此时,即使数据未在文本框中归档,也会出现错误 WebDriverWait(driver, 10).until(driver.find_element_by_xpath("//*[@id='txtFrom']").send_keys("Delhi, India",Keys.DOWN)) NameError: name 'Keys' is not defined

这是我的完整代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

path_to_chromedriver = 'C:/Users/prash/Desktop/WebScrap/selenium/chromedriver' 

driver = webdriver.Chrome(executable_path = path_to_chromedriver)
driver.implicitly_wait(10) 
driver.maximize_window()
url = 'http://tis.nhai.gov.in/tollplazasonmap?language=en'
driver.get(url) 
embed = driver.find_element_by_tag_name('embed') 
driver.switch_to.frame(embed) 
element = driver.find_element_by_id('tollstation') 
driver.execute_script("arguments[0].click();", element)
WebDriverWait(driver, 10).until(driver.find_element_by_xpath("//*[@id='txtFrom']").send_keys("Delhi, India",Keys.DOWN))
WebDriverWait(driver, 10).until(driver.find_element_by_xpath("//*[@id='txtTo']").send_keys("Bangalore",Keys.DOWN))
element2 = driver.find_element_by_xpath("//*[@id='showstation']/p[7]/a")
driver.execute_script("arguments[0].click();", element2)

1 个答案:

答案 0 :(得分:0)

为了能够使用Keys module,您应首先将其导入为

from selenium.webdriver.common.keys import Keys

之后,您可以在代码中使用Keys.TABKeys.DOWN