在搜索框中输入文本,然后从“使用Selenium和Python自动完成"中进行选择 [英] Inputting text in search box and then selecting from Auto Complete with Selenium and Python
问题描述
我正在尝试使用硒和python在搜索框中输入一些文本.代码如下.如果选择高级联赛"选项,它将运行得非常好.尽管输入此文本后,它作为预定义选项出现在框中,但我无法根据需要选择并单击它...如果要浏览网页,则可以单击高级联赛"选项,加载新页面.我似乎无法复制此内容
I am trying to input some text in a search box using selenium and 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')
推荐答案
当您尝试通过classname
标识Search Box
时,标识如下:
As you have tried to identify the Search Box
through classname
identifies as follows :
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')
这些定位器不能唯一地标识Search Box
.
These locators doesn't identifies the Search Box
uniquely.
对于input some text in a search box
,您必须通过唯一的CSS
或XPATH
来标识占位符设置为Enter a team or competition
的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 :
driver.find_element_by_xpath("//input[@id='sp-c-search__input']").send_keys("premier league")
最后,对于文本为 Premier League
的选项中的click()
,可以使用以下代码块:
Finally, to click()
on the option with text as Premier League
you can use the following code block :
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
这篇关于在搜索框中输入文本,然后从“使用Selenium和Python自动完成"中进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!