Search code examples
pythonselenium-webdriverweb-scrapingselenium-chromedriver

Click a dropdown search result item at random location in the dropdown result python selenium library


I have been trying my head around to select the searched elements in the screenshot below. I tried the below methods none of them seems to be able to find the dropdown searched results elements.

enter image description here Sharing the corresponding source code of the dropdown result below based on inspecting the result.

enter image description here

Following methods I have tried so far using selenium python library.

    span_class="Roboto--ezzN4 text-5--NzQT7"
    tag_name = "aria-label"
    drop_down_tag_result = driver.find_element(by="class name", value=span_class)
    drop_down_tag_result = driver.find_element(by="tag name", value=tag_name)    
    
    drop_down_result = driver.find_element(by="xpath", value="//div[text()="+"'"+brand_name+"'"+"]")
    drop_down_result.click()

All of above three methods can't locate the element and getting similar error below.

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".Roboto--ezzN4 text-5--NzQT7"}
  (Session info: chrome=120.0.6099.109); 

Can anyone help me guide in the right direction? I am new to selenium.


Solution

  • There might possibility of synchronization issue. use explicit wait before interacting the element with expected condition see the example here --> Waits

    Please change the xpath to identify the element uniquely.

    drop_down_result = driver.find_element(by="xpath", value="//span[text()='" + brand_name + "']")
    

    or

    drop_down_result = driver.find_element(by="xpath", value="//div[@aria-label='" + brand_name + "']")