为什么幻像驱动程序在选择选项时会进入无限循环

时间:2018-03-06 19:43:03

标签: java selenium selenium-webdriver phantomjs chromium

在网页上选择类型:

<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标签中选择“X Y Z Types”值。我尝试用以下方式选择:

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)

更新:(正如比尔所说,它可能是页面加载问题)当我尝试手动访问此网页时,我确实看到微调器持续旋转。 (下图)

enter image description here

enter image description here

更新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();

1 个答案:

答案 0 :(得分:0)

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

[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 ExpectedConditions子句设置为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();