1

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")

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
sky_bird
  • 247
  • 1
  • 4
  • 13
  • Possible duplicate of [Using selenium (python) to select an option from a conditional dropdown](https://stackoverflow.com/questions/35068238/using-selenium-python-to-select-an-option-from-a-conditional-dropdown) – JeffC Aug 10 '17 at 16:51

3 Answers3

2

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

what you're typing is not valid Python. Are you looking at an actual Python example?

## not valid Python code.
web_element x = driver.find_element_by_xpath('//*[@id="txtSearchPhone"]')
Select sel = new Select(x)
sel.select_by_value("Iphone")

what you need to do is find the select box...

select_box = driver.find_element_by_xpath('//*[@id="yourSelectBoxId"]')

than iterate your select box...

for i in select_box:
    if i.text == "some text":
        i.click();

Before all of that, you probably need to spend some time learning the Python language.

Chris Hawkes
  • 11,923
  • 6
  • 58
  • 68
1

Python is a Dynamically typed language, you don't need to specify the type of the variable when you declare a variable.

a=10 creates a int and

name="hello world" creates a string

Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137