0

I'm building a Selenium Chrome bot using Python. I implement python coroutine a sync function. Using Selenium wait method with python a sync function.

I got an error which behavior is not what I expect.

I select item on drop-down A, wait for few seconds, so that drop-down B will be populated based on item's value in drop-down A.

This process wraps inside python a sync function with await. It doesn't loop according to this behavior.

But run all at once.

async def

regionLists = ['111','222','333']
regionElem = self.browser.find_element_by_id("regionId")
regionSelect = Select(regionElem)
regionTasks = [
  self.parseCities(self.browser, regionSelect, regionValue)
  for regionValue in regionLists
]

result = await asyncio.wait(regionTasks)

ParseCities function

async def parseCities(self, browser, regionSelect, regionValue):
  regionSelect.select_by_value(regionValue)

  # ERROR AT THIS LINE BELOW
  wait(browser, 10).until(
      lambda b: len(Select(b.find_element_by_id("cityId")).options) > 1
  )

Error

Task exception was never retrieved
future: <Task finished coro=<main.parseCities() done, defined at Selenium/main.py:139> exception=NoSuchElementException('Cannot locate option with value: 781111')>
Traceback (most recent call last):
  File "Selenium/main.py", line 140, in parseCities
    regionSelect.select_by_value(regionValue)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/support/select.py", line 87, in select_by_value
    raise NoSuchElementException("Cannot locate option with value: %s" % value)
selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 781111

1 Answer 1

3

Try to wait for the element to be clickable

Just example

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

 element = WebDriverWait(driver, 30).until(
    ec.element_to_be_clickable((By.ID, 'cityId')))

  element.click()
Sign up to request clarification or add additional context in comments.

Comments

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.