This code is working for color swatch attribute.
Using below code, first swatch are automatically selected after page load in details page,
you have to override file into your theme,
app/design/frontend/Packagename/themename/Magento_Swatches/templates/product/view/renderer.phtml
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<?php /** @var $block \Magento\Swatches\Block\Product\Renderer\Configurable */ ?>
<div class="swatch-opt"></div>
<script>
require(["jquery", "jquery/ui", "swatchRenderer"], function ($) {
$('.swatch-opt').SwatchRenderer({
jsonConfig: <?php /* @escapeNotVerified */ echo $swatchOptions = $block->getJsonConfig(); ?>,
jsonSwatchConfig: <?php /* @escapeNotVerified */ echo $swatchOptions = $block->getJsonSwatchConfig(); ?>,
mediaCallback: '<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>'
});
//for selected option if only one value is exist....
jQuery(document).ready(function(){
//for swatches1
var swatchLength = $('.swatch-attribute').length;
if(swatchLength >= 1){
if($('.swatch-attribute').hasClass("color")){
$('.swatch-option').first().trigger('click');
}
}
});
});
</script>