我正在使用Python构建Selenium Chrome机器人。我实现了python协程的同步功能。使用Selenium wait方法和python的同步功能。
我遇到了一个错误,该错误不是我的预期。
我在下拉菜单A上选择了项目,请等待几秒钟,以便根据下拉菜单A中的项目值填充下拉菜单B。
此过程在python中包装了一个带有await的同步函数。它不会根据此行为循环。
但是一次全部运行。
异步定义
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功能
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
)
错误
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
答案 0 :(得分:0)
尝试等待元素可点击
只是示例
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()