2

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
1

4 Answers 4

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()
Sign up to request clarification or add additional context in comments.

5 Comments

I am still getting an error, 'Message: Select only works on <select> elements, not on <input>'
The error says it all. The element returned by driver.find_element_by_id("txtSearchPhone") is an input tag not a select tag. How can you apply Select sel = new Select(x) on it?
can you share the relevant HTML or the url if it is a public one?
tradein.vodafone.co.uk/#/topmodel . I am entering Apple iPhone 6 128GB in the search box and it appears as a dropdown. I want to select that.
Thanx a lot!!:)
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.

6 Comments

Yeah, i got that. Can i directly have Iphone as an option, instead of creating a loop?
Not that I'm aware of. Some dropdowns these days are loaded when they are clicked, it really depends on the site. Most of the time, I need to iterate the select box values to select the right text or value which matches what I'm looking for.
Its showing an error of 'Webelement is not iterable'
that's because you must have selected an element that was not a select box.
yes, it was a search box rather than a drop down box
|
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

1 Comment

Thanx, got that!. But that hasn't solved my problem.
0

You can use the following code block to select the option "MUMBAI" from the suggestions:

def autocomplete_select_Submission(self, by, value, text, timeout=10):
    """Selects an option from an autocomplete dropdown by typing and choosing the first suggestion."""
    input_field = WebDriverWait(self.driver, timeout).until(
        EC.visibility_of_element_located((by, value))
    )
    input_field.clear()
    input_field.send_keys(text)  # Enter text

    time.sleep(3)  # Increase time for dropdown to load

    # Wait until at least one suggestion appears
    suggestions = WebDriverWait(self.driver, timeout).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, "ui-menu-item"))
    )

    for suggestion in suggestions:
        print(f"Suggestion found: {suggestion.text}")  # Debugging
        if text.upper() in suggestion.text.upper():
            suggestion.click()
            print(f"Selected {text} from autocomplete")
            return

    # If no exact match, try arrow key + Enter
    input_field.send_keys(Keys.DOWN)
    input_field.send_keys(Keys.ENTER)
    print(f"{value} updated with {text} using keyboard")

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.