为什么幻影驱动程序在选择一个选项时进入无限循环

Why is phantom driver going in infinite loop while selecting an option

Select 在网页上输入:

<select id="FILE_TYPE" data-context="Input" onchange="filterValueChanged('FILE_TYPE','false','-##ALL##-');display_submitForm('DEPENDENCY','','DISPLAY_6061638','SOURCE_CONTROL_ID=FILE_TYPE' );">
<option value="-##ALL##-" selected="">[ALL]</option>
<option value="AB_CD">AB CD Types</option>
<option value="CA_BD">CA BD Types</option>
<option value="CONFIG">Configuration Types</option>
<option value="XYZ">X Y Z Types</option>
<option value="ST_MT">ST MT Types</option>

查找元素的代码工作正常:

Select selectBox = new Select(driver.findElement(By.id("FILE_TYPE")));
System.out.println(selectBox.getOptions().size()); // printing "6" 
selectBox.getOptions().forEach(e -> getValue(e)); // printing all options text

现在我想在 select 标签中 select "X Y Z Types" 值。我尝试了 select 以下方法:

1) 指数

selectBox.selectByIndex(4);

2) 可见文本

selectBox.selectByVisibleText("X Y Z Types");

3) 值

selectBox.selectByValue("XYZ");

4) XPATH

driver.findElement(By.xpath("//[@id=\"FILE_TYPE\"]/option[5]")).click();

但是没有任何效果,而是它正在控制无限循环。

[DEBUG - 2018-03-06T19:36:22.614Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
[DEBUG - 2018-03-06T19:36:22.692Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - page.onConsoleMessage - start
[DEBUG - 2018-03-06T19:36:22.692Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - page.onConsoleMessage - is topWindow
[DEBUG - 2018-03-06T19:36:22.692Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - page.onConsoleMessage - check if window registered
[DEBUG - 2018-03-06T19:36:22.692Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - page.onConsoleMessage - window found: true
[DEBUG - 2018-03-06T19:36:22.723Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
[DEBUG - 2018-03-06T19:36:22.832Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
[DEBUG - 2018-03-06T19:36:22.942Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
[DEBUG - 2018-03-06T19:36:23.050Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
[DEBUG - 2018-03-06T19:36:23.159Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
[DEBUG - 2018-03-06T19:36:23.269Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
....(infinitely)

更新:(正如 Bill 所建议的,这可能是页面加载问题)当我尝试手动访问此网页时,我确实看到微调器不断旋转。 (下图)

更新 2: 添加驱动程序和浏览器的初始化(根据 DebanjanB 的建议)

System.setProperty("webdriver.chrome.driver", seleniumDriverPATH);
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
ImmutableMap<String, String> commandLineArguments = ImmutableMap.<String, String>builder()
        .put("web-security", "false")
        .put("ssl-protocol", "any")
        .put("ignore-ssl-errors", "true")
        .put("webdriver-loglevel", "DEBUG")
        .put("ssl-client-certificate-file", certificatePath)
        .put("ssl-client-key-passphrase", certificatePassword).build();
String[] params = commandLineArguments.entrySet().stream()
        .map(e -> String.format("--%s=%s", e.getKey(), e.getValue())).collect(Collectors.toList())
        .toArray(new String[0]);
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, params);
cap.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new PhantomJSDriver(cap);

更新 3:我尝试使用 IE 11(未显示加载微调器)但仍然遇到相同的问题 - 单击下面的行时控制进入无限循环

new WebDriverWait(driver, 20).until(ExpectedConditions. elementToBeClickable(By.xpath("//select[@id='FILE_TYPE' and @data-context='Input']//option[@value='XYZ']"))).click();

从您的问题更新和快照可以清楚地看出页面尚未完成加载,因此您会看到以下错误;

[DEBUG - 2018-03-06T19:36:22.614Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true
[DEBUG - 2018-03-06T19:36:22.692Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - page.onConsoleMessage - start
[DEBUG - 2018-03-06T19:36:22.692Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - page.onConsoleMessage - is topWindow
[DEBUG - 2018-03-06T19:36:22.692Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - page.onConsoleMessage - check if window registered
[DEBUG - 2018-03-06T19:36:22.692Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - page.onConsoleMessage - window found: true
[DEBUG - 2018-03-06T19:36:22.723Z] Session [91f59080-2175-11e8-9d57-a93d0ef995b3] - _execFuncAndWaitForLoadDecorator - Page Loading in Session: true

解决方案

一个可能的解决方案是按如下方式诱导 WebDriverWait with ExpectedConditions clause set to elementToBeSelected :

Select selectBox = new Select(driver.findElement(By.id("FILE_TYPE")));
System.out.println(selectBox.getOptions().size()); // printing "6" 
selectBox.getOptions().forEach(e -> getValue(e)); // printing all options text
new WebDriverWait(driver, 20).until(ExpectedConditions. elementToBeClickable(By.xpath("//select[@id='FILE_TYPE' and @data-context='Input']//option[@value='XYZ']"))).click();