-1

In my application i have a text box which when i enter the city name brings the city names to select a city.when i am trying to locate the element,it says it is unable to locate the element.

THis is HTML code

<ul id="autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19" class="autocomplete-content
dropdown-content" tabindex="0" style="display: block; width: 231.948px; left: 5px; top: 54px; 
height: 50px; transform-origin: 0px 0px; opacity: 1; transform: scaleX(1) scaleY(1);">
<li class="active"><span><span class="highlight">Hyderabad</span></span></li></ul>

Here is my script code

driver.find_element_by_name('residential_city_id').send_keys("Hyderabad")
tme.sleep(5)
html_list=driver.find_element_by_id("autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19")
items = html_list.find_elements_by_tag_name("li")
for item in items:
    text = item.text
    print(text)

i am getting the following error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate 
element: {"method":"css selector","selector":"[id="autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19"]"}
  (Session info: chrome=76.0.3809.132)
DebanjanB
  • 118,661
  • 30
  • 168
  • 217

2 Answers2

0

I think your issue is in these lines of code here:

html_list=driver.find_element_by_id("autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19")
items = html_list.find_elements_by_tag_name("li")

The ID autocomplete-options-fd419e03-fc56-725a-dc68-0dd3e55a2b19 looks like a randomized ID that changes each time you refresh the page. You want to use a more robust selector in this case.

I don't think there is a need to get items list at all. I think you can replace the two lines of code above with just one:

driver.find_element_by_xpath("//span[text()='Hyderabad']")

This will find the span element with text Hyderabad

Now your code will look like:

driver.find_element_by_name('residential_city_id').send_keys("Hyderabad")
tme.sleep(5)
driver.find_element_by_xpath("//span[text()='Hyderabad']")
Christine
  • 5,567
  • 2
  • 12
  • 35
  • don't use sleeps, use waits. – DMart Sep 25 '19 at 20:17
  • I copied sleep from the code posted by the asker. They can change it to wait if they would prefer. – Christine Sep 25 '19 at 20:22
  • raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"li[text()='Hyderabad']"} (Session info: chrome=76.0.3809.132) – Swarna Mera Sep 26 '19 at 06:18
  • Updated XPath with `span` instead of `li`, I misread the HTML initially and it looks like the city text is inside the span element. – Christine Sep 26 '19 at 12:53
  • still the same exception was coming.unable to locate the element – Swarna Mera Oct 01 '19 at 18:09
  • @SwarnaMera I updated my XPaths to use `//` notation. Could you try with this: `driver.find_element_by_xpath("//span[text()='Hyderabad']")` – Christine Oct 01 '19 at 19:05
  • Thank You Christine,The code was working.I appreciate it. – Swarna Mera Oct 02 '19 at 19:25
  • @SwarnaMera Glad to hear it. If this solution has helped you, feel free to mark as answer to your question so that other users can see how you resolved your issue. – Christine Oct 02 '19 at 19:26
0

To click() on the auto complete element with text as Hyderabad you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.autocomplete-content.dropdown-content li.active > span > span.highlight"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='autocomplete-content dropdown-content']//li[@class='active']/span/span[@class='highlight' and text()='Hyderabad']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217