简体   繁体   English

在搜索框中输入文本,然后从Selenium和Python自动完成中选择

[英]Inputting text in search box and then selecting from Auto Complete with Selenium and Python

I am trying to input some text in a search box using selenium and python. 我正在尝试使用硒和python在搜索框中输入一些文本。 The code is below. 代码如下。 It runs perfectly well up until the point if selecting the 'premier league' option. 如果选择“高级联赛”选项,它将运行得很好。 Although when this text is entered it comes up in the box as a pre-defined option I am unable to select and click it as I want...If you are navigating the webpage you can click on the 'premier league' option and it loads a new page. 尽管输入此文本后,它作为预定义选项出现在框中,但我无法根据需要选择并单击它...如果要浏览网页,则可以单击“高级联赛”选项,加载新页面。 I don't seem to be able to replicate this 我似乎无法复制

import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchAttributeException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

browser = webdriver.Firefox()
browser.get("http://www.bbc.co.uk/sport/football/scores-fixtures")


try:
    elem = browser.find_element_by_class_name('sp-c-date-picker-timeline__item')
except:
    print('Was not able to find an element with that name.')

time.sleep(5)
elem.click()


try:
    elem2=browser.find_element_by_class_name('sp-c-search__input')
except:
    print('Not able to find an element with that name')

time.sleep(3)

elem2.send_keys('premier league')
time.sleep(2)

#searching for the text works but i can't select it

#elem2.send_keys(Keys.DOWN)
#time.sleep(2)
#elem2.send_keys(Keys.DOWN)
#time.sleep(2)
#elem2.send_keys(Keys.RETURN)
#elem2.submit()

elem2.select_by_visible_text('Premier League')

As you have tried to identify the Search Box through classname identifies as follows : 当您尝试通过classname标识“ Search Box ,标识如下:

elem = browser.find_element_by_class_name('sp-c-date-picker-timeline__item')
#and
elem2=browser.find_element_by_class_name('sp-c-search__input')

These locators doesn't identifies the Search Box uniquely. 这些定位器不能唯一地标识Search Box

To input some text in a search box you have to identify the Search Box with placeholder set to Enter a team or competition through a unique CSS or XPATH as follows : input some text in a search box您必须确定“ Search Box中的占位符设置为Enter a team or competition通过唯一的CSSXPATH Enter a team or competition ,如下所示:

driver.find_element_by_xpath("//input[@id='sp-c-search__input']").send_keys("premier league")

Finally, to click() on the option with text as Premier League you can use the following code block : 最后,要在文本为Premier League的选项上click() ,可以使用以下代码块:

suggestions = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located(By.XPATH,"//input[@id='sp-c-search__input']//following::div[@id='search-results']//ul/li/a/mark"))
for suggestion in suggestions :
    if suggestion.get_attribute("innerHTML").contains("Premier League") :
        suggestion.click()

Update 更新

As you are seeing the error as : 如您所见,错误为:

NameError: name 'By' is not defined

Ensure that you have added all the following imports : 确保已添加以下所有导入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM