硒无法找到搜索字段

时间:2018-11-05 18:43:23

标签: python selenium ui-automation python-behave

这是网站-https://www.phptravels.net/m-hotels

我找不到名为“城市”字段的字段(请参见所附的屏幕截图),甚至硒也无法记录该字段。我想在该字段中输入文本。

enter image description here

这是我尝试过的方法,但是没有用:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.phptravels.net/m-hotels")
a = driver.find_element_by_class_name("select2-choice")
a.click()
city = driver.find_element_by_css_selector(".select2-input.select2-focused")
city.send_keys("Test")

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

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

driver = webdriver.Chrome()
driver.get("https://www.phptravels.net/m-hotels")

wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Search by Hotel or City Name"))).click()
wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#select2-drop .select2-input"))).send_keys("Test")

答案 1 :(得分:1)

使用xpath的另一种选择

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.phptravels.net/m-hotels")
a = driver.find_element_by_class_name("select2-choice")
a.click()
city = driver.find_elements_by_xpath('(//*[@id="select2-drop"]/div/input)')
city[0].send_keys("Jakarta")