这是我当前的Python脚本的一部分:
Select(driver.find_element_by_id("FFQAuto_Vehicle_vehicleYear")).select_by_visible_text("2015")
time.sleep(4)
Select(driver.find_element_by_id("FFQAuto_Vehicle_vehicleMake")).select_by_visible_text("HONDA")
time.sleep(4)
Select(driver.find_element_by_id("FFQAuto_Vehicle_vehicleModel")).select_by_visible_text("CIVIC 4D LX")
我想摆脱time.sleep(4)
并改用WebDriverWait
。我广泛使用WebDriverWait
,但从来不需要等待列表填充的地方。我已经尝试了很多组合,但是都没有用。
有人知道该怎么做吗?
谢谢
我尝试了以下操作,但是没有用:
vehicleYear = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "FFQAuto_Vehicle_vehicleYear"))))
vehicleYear.select_by_visible_text("2015")
vehicleMake = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "FFQAuto_Vehicle_vehicleMake"))))
vehicleMake.select_by_visible_text("HONDA")
vehicleModel= Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "FFQAuto_Vehicle_vehicleModel"))))
vehicleModel.select_by_visible_text("CIVIC 4D LX")
以下是异常消息:
Traceback (most recent call last):
File "c:\Users\uswarv41\Box Sync\Documents\GitHub\appd-selenium-scripts\Digital_FFQ_PROD_Auto_Synthetic_Script.py", line 169, in <module>
vehicleMake.select_by_visible_text("HONDA")
File "C:\Users\uswarv41\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\select.py", line 140, in select_by_visible_text
raise NoSuchElementException("Could not locate element with visible text: %s" % text)
selenium.common.exceptions.NoSuchElementException: Message: Could not locate element with visible text: HONDA
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\ptvsd_launcher.py", line 45, in <module>
main(ptvsdArgs)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 265, in main
wait=args.wait)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\__main__.py", line 256, in handle_args
run_main(addr, name, kind, *extra, **kwargs)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_local.py", line 52, in run_main
runner(addr, name, kind == 'module', *extra, **kwargs)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\runner.py", line 32, in run
set_trace=False)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1283, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 1290, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "c:\Users\uswarv41\.vscode\extensions\ms-python.python-2018.12.1\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "c:\Users\uswarv41\Box Sync\Documents\GitHub\appd-selenium-scripts\Digital_FFQ_PROD_Auto_Synthetic_Script.py", line 181, in <module>
我相信我已经通过问题解决了。创建了以下功能:
def loop_select_keys(eName, eValue):
attempt = 1
while True:
try:
time.sleep(0.5)
return Select(driver.find_element_by_id(eName)).select_by_visible_text(eValue)
print(attempt)
except (StaleElementReferenceException, ElementNotInteractableException, MoveTargetOutOfBoundsException, NoSuchElementException, InvalidElementStateException) :
if attempt == 10:
raise
attempt += 1
我这样调用函数:
elementName = "FFQAuto_Vehicle_vehicleYear"
elementValue = "2015"
loop_select_keys(elementName,elementValue)
elementName = "FFQAuto_Vehicle_vehicleMake"
elementValue = "HONDA"
loop_select_keys(elementName,elementValue)
elementName = "FFQAuto_Vehicle_vehicleModel"
elementValue = "CIVIC 4D LX"
loop_select_keys(elementName,elementValue)