在python中使用Selenium Web驱动程序从自动完成下拉菜单中选择选项 [英] Selecting options from Auto Complete Dropdown using selenium web driver in python
本文介绍了在python中使用Selenium Web驱动程序从自动完成下拉菜单中选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须从页面的下拉菜单中选择选项.我尝试了以下代码,但显示语法错误.有人可以帮我吗?
I have to select options from a drop down on a page. I tried the below code but it is showing a syntax error. Can somebody help me with this?
web_element x = driver.find_element_by_xpath('//*[@id="txtSearchPhone"]')
Select sel = new Select(x)
sel.select_by_value("Iphone")
我也尝试使用Web_element和WebElement. 但这显示出第一行的语法错误.
I tries Web_element, WebElement too. But this is showing syntax error for the very first line.
web_element x = driver.find_element_by_xpath('//*[@id="txtSearchPhone"]')
^
SyntaxError: invalid syntax
推荐答案
您可以使用以下代码块从建议中选择选项Apple iPhone 6 128GB
:
You can use the following code block to select the option Apple iPhone 6 128GB
from the suggestions:
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
driver = webdriver.Chrome(executable_path= r"C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.maximize_window()
driver.get('https://tradein.vodafone.co.uk/#/topmodel')
driver.find_element_by_xpath("//input[@id='txtSearchPhone']").send_keys("Apple iPhone 6 128GB")
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='ui-menu-item']/a[contains(@id, 'ui-id-')][text()='Apple iPhone 6 128GB']")))
driver.find_element_by_xpath("//li[@class='ui-menu-item']/a[contains(@id, 'ui-id-')][text()='Apple iPhone 6 128GB']").click()
这篇关于在python中使用Selenium Web驱动程序从自动完成下拉菜单中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文